网格视图不刷新

时间:2015-08-10 13:10:16

标签: android gridview

我遇到了gridview的问题。我正在使用适配器中每个项目(月)的自定义适配器和自定义视图。 http://cs622428.vk.me/v622428086/3f452/K30qwCnc-iQ.jpg

当我转到另一个片段然后按回来时,gridview消失。 http://cs622428.vk.me/v622428086/3f45c/2sbbchFTYgM.jpg

=============================================== ==================

抱歉纠结的代码。当我遇到问题时只有2个片段,但后来我决定每次需要更新时都尝试替换整个片段。但即使我在第一个活动中调用onResume并替换所有内容,它仍然无法工作......

那我怎么刷新呢?它不起作用:

@Override
public void onResume() {
    super.onResume();
    gridViewAdapter = new GridViewAdapter(getActivity(), currentYear.getMonths());
    gridView.setAdapter(gridViewAdapter);
    gridViewAdapter.notifyDataSetChanged();
    gridView.setOnItemClickListener(this);
}

适配器:

public class GridViewAdapter extends ArrayAdapter<Month> {

private Context context;
private ArrayList<Month> months;

public GridViewAdapter(Context context, ArrayList<Month> months) {
    super(context, 0, months);
    this.context = context;
    this.months = months;

}

public ArrayList<Month> getData() {
    return months;
}



@Override
public int getCount() {
    return months.size();
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    view = new DrawSingleMonthView(context, months.get(i));
    return view;
    //return new DrawSingleMonthView(context, months.get(i));
}

标志 - 控制器。我打电话给单月(双击)或所有月份。

public class CalendarFragment extends Fragment implements OnClickCalendarListener {

private FragmentTransaction fTrans;
private Year year;
private Year secondYear;
private Year thirdYear;
private Year currentYear;

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

    Calendar calendar = GregorianCalendar.getInstance();

    year = new Year(calendar.get(Calendar.YEAR));
    secondYear = new Year(calendar.get(Calendar.YEAR) + 1);
    thirdYear = new Year(calendar.get(Calendar.YEAR) + 2);

    currentYear = year;

    View v = inflater.inflate(R.layout.fragment_calendar_container, container, false);


    return v;
}

@Override
public void onResume() {
    super.onResume();
    fTrans = getFragmentManager().beginTransaction();
    AllMonthsFragment monthsFragment = new AllMonthsFragment(year, this);
    fTrans.replace(R.id.fragment_calendar_container, monthsFragment);
    fTrans.commit();
    MonthFragment.addListener(this);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


}

@Override
public void onDoubleCalendarClick(int position) {
    fTrans = getFragmentManager().beginTransaction();
    SingleMonthFragment calendar = new SingleMonthFragment(currentYear.getMonths().get(position));
    fTrans.replace(R.id.fragment_calendar_container, calendar);
    fTrans.addToBackStack(null);
    fTrans.commit();
}

这是我在FrameLayout上替换所有月份的片段。

public class AllMonthsFragment extends Fragment implements View.OnClickListener{

private TextView yearTW;
private View v;
private FragmentTransaction fTrans;
private FrameLayout frameLayout;
private Year year;
private boolean flag;
CalendarFragment calendarFragment;

public AllMonthsFragment(Year year) {
    this.year = year;
    flag = false;
}

public AllMonthsFragment(Year year, CalendarFragment calendarFragment) {
    this.year = year;
    this.calendarFragment = calendarFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.calendar_all_month, container, false);

    return v;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    yearTW = (TextView) v.findViewById(R.id.yearTW);
    yearTW.setText("" + year.getNumberOfYear());
    yearTW.setOnClickListener(this);

    frameLayout = (FrameLayout) v.findViewById(R.id.container_months);

}

private void openCalendarMonthFragment () {
    fTrans = getFragmentManager().beginTransaction();
    MonthFragment monthFragment = new MonthFragment(year.getMonths());
    fTrans.replace(R.id.container_months, monthFragment);
    fTrans.commit();

}

@Override
public void onPause() {
    super.onPause();
    flag = true;
}

@Override
public void onResume() {
    super.onResume();
    if(flag) {
        calendarFragment.onResume();
    }
    openCalendarMonthFragment();
}

@Override
public void onClick(View view) {

}

带有gridlayout的片段。

public class MonthFragment extends Fragment implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {


private FragmentTransaction fTrans;
private View v;
private GridView gridView;
private GridViewAdapter gridViewAdapter;
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
private long lastClickTime = 0;
private ArrayList<Month> months;
private static List<OnClickCalendarListener> listeners;

MonthFragment (ArrayList<Month> months) {
    this.months = months;
}

static {
    listeners = new ArrayList<>();
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_calendar_all_month, container, false);

    gridView = (GridView) v.findViewById(R.id.gridView);
    adjustGridView();

    gridViewAdapter = new GridViewAdapter(getActivity(), months);
    gridView.setAdapter(gridViewAdapter);

    gridView.setOnItemClickListener(this);
    gridView.setOnItemLongClickListener(this);
    return v;
}

public static void addListener(OnClickCalendarListener onClickCalendarListener) {
    listeners.add(onClickCalendarListener);
}

private static void notifyDoubleClick(int i) {
    for (OnClickCalendarListener onClickCalendarListener : listeners)
        onClickCalendarListener.onDoubleCalendarClick(i);
}

private void adjustGridView() {
    gridView.setVerticalScrollBarEnabled(false);
}

private void onSingleClick(View v, int i) {
    if (months.get(i).isSelected())
        months.get(i).setSelected(false);
    else months.get(i).setSelected(true);
    v.invalidate();
}

private void onDoubleClick(int i) {
    notifyDoubleClick(i);
}

@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
    onDoubleClick(i);
    return false;
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    long clickTime = System.currentTimeMillis();
    if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA) {
        onDoubleClick(i);
    } else {
        onSingleClick(v, i);
    }
    lastClickTime = clickTime;
    view.invalidate();

1 个答案:

答案 0 :(得分:0)

用户runOnUi线程.. !!!在setOnItemClickListner .. !!

 runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    gridViewAdapter.notifyDataSetChanged();
                }
            });

希望这有帮助。!