0

我有一个用户可以选择的数字列表。一旦选择了一个数字,我想保存该数字的选定状态,并能够在应用程序重新启动时恢复这些数字的状态。

目前我很难让 GridView 中的项目在onCreateView被调用时恢复它们的状态。我试过打电话gridView.getChildCount(),我注意到它返回 0。我已经阅读了许多有类似问题的线程并尝试了他们的答案,但没有成功。我已经做了几天了。

片段1.java:

public class Fragment1 extends Fragment implements OnItemClickListener {

public static final String ARG_SECTION_NUMBER = "section_number";

private static final int START_NUM = 1;
private static final int END_NUM = 59;
public static boolean[] num_select = new boolean[END_NUM - START_NUM + 1];

private static GridView gridView;
private static NumberAdapter adapter;

public Fragment1() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_1, container, false);

    gridView = (GridView) rootView.findViewById(R.id.gridview_number);

    adapter = new NumberAdapter(getActivity(), START_NUM, END_NUM);
    gridView.setAdapter(adapter);
    //gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);

    gridView.setOnItemClickListener((OnItemClickListener) this);

    // ************* I NEED TO BE ABLE TO RESTORE THE NUMBER STATE HERE *******
//restoreNumbers();

    return rootView;
}   

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

num_select[position] = !num_select[position];

    if (num_select[position])
    {
        v.getBackground().setColorFilter(Color.rgb(210, 230, 230), 
    }
    else
    {
        v.getBackground().clearColorFilter();           
    }       
}

NumberAdapter.java:

public class NumberAdapter extends BaseAdapter {
private Context context;
private int min_num;
private int max_num;    

public NumberAdapter(Context c, int num1, int num2) {
    this.context = c;        
    this.min_num = num1;
    this.max_num = num2;

    total_num = max_num - min_num + 1;       
}    

public View getView(int position, View convertView, ViewGroup parent) {

    TextView textView;

    if(convertView == null)
    {
        textView = new TextView(context);
        textView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 85));
        textView.setTextSize(18.0f);
        textView.setGravity(0x11);
    }
    else
    {
        textView = (TextView) convertView;
    }    

    textView.setText(Integer.toString(position));
    textView.setBackgroundResource(R.drawable.number_deselect);  

    return textView;
}

public int getCount() {
    return total_num;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return position;
}

}

4

1 回答 1

1

多亏了这个帖子,我找到了答案。基本上,您需要在 GridView 完成绘制后创建一个 addOnGlobalLayoutListener 并调用 getChildAt。如果过早调用它,它将返回 null 的视图。

ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        TextView textView = (TextView) gridView.getChildAt(i);
        textView.getBackground().setColorFilter(Color.rgb(210, 230, 230), PorterDuff.Mode.MULTIPLY);
    }
});
于 2013-10-13T10:38:23.053 回答