ListView的适配器仅在从backstack恢复片段后显示第一个项目的文本

时间:2017-06-09 19:37:07

标签: android listview android-fragments

我有一个ListView适配器,显示包含单词的卡片列表。通过拨打notifyDataSetChanged(),可以拖出或放入这些卡片。当我从后面的堆栈返回到这个片段的实例时,只有列表中的第一张卡片的textview设置为text。我应该补充一点,当我在ViewHolder对象上设置文本之前在适配器的notifyDataSetChanged()中调用getView()时,文本会在明显的延迟(稍微小于.5秒)之后出现。这是适配器:

public class ItemListAdapter extends ItemBaseAdapter {

public ItemListAdapter(Context context, List<WordCard> wordCards) {
    super(context, wordCards);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View rowView = convertView;

    // reuse views
    if (rowView == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();

        rowView = inflater.inflate(R.layout.item_word, null);

       ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) rowView.findViewById(R.id.word_text);
        viewHolder.mCloseBtn =  (ImageView)rowView.findViewById(R.id.close_btn);
        rowView.setTag(viewHolder);
    } else {
        ViewHolder holder = (ViewHolder) rowView.getTag();
        holder.text.setText(mWordCards.get(position).getWord());
        holder.mCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWordCards.remove(position);
                notifyDataSetChanged();
            }
        });
        rowView.setOnDragListener(new ItemDragListener(mWordCards.get(position)));
    }

    return rowView;
}

static class ViewHolder {
    TextView text;
    ImageView mCloseBtn;
}
}

它在片段的onCreateView中设置:

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

    setHasOptionsMenu(true);

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

    return rootView;
}


@Override
public void onResume() {
    super.onResume();
    setRetainInstance(true);
}

private void init(View rootView) {
    mGamePresenter = new GamePresenter(this);

    mListView1 = (ListView) rootView.findViewById(R.id.listview1);
    mListView2 = (ListView) rootView.findViewById(R.id.listview2);
    mListView3 = (ListView) rootView.findViewById(R.id.listview3);
    mListView4 = (ListView) rootView.findViewById(R.id.listview4);
    mGridView = (GridView) rootView.findViewById(R.id.card_grid);

    mGridEmptyStateLl = (LinearLayout) rootView.findViewById(R.id.grid_empty_state_ll);

    mCodePanel1 = (LinearLayoutAbsListView) rootView.findViewById(R.id.code_panel1);
    mCodePanel1.setOnDragListener(new ViewDragListener(mGamePresenter));
    mCodePanel1.setAbsListView(mListView1);
    mCodePanel2 = (LinearLayoutAbsListView) rootView.findViewById(R.id.code_panel2);
    mCodePanel2.setOnDragListener(new ViewDragListener(mGamePresenter));
    mCodePanel2.setAbsListView(mListView2);
    mCodePanel3 = (LinearLayoutAbsListView) rootView.findViewById(R.id.code_panel3);
    mCodePanel3.setOnDragListener(new ViewDragListener(mGamePresenter));
    mCodePanel3.setAbsListView(mListView3);
    mCodePanel4 = (LinearLayoutAbsListView) rootView.findViewById(R.id.code_panel4);
    mCodePanel4.setOnDragListener(new ViewDragListener(mGamePresenter));
    mCodePanel4.setAbsListView(mListView4);
    mGridPanel = (LinearLayoutAbsListView) rootView.findViewById(R.id.grid_panel);
    mGridPanel.setOnDragListener(new ViewDragListener(mGamePresenter));
    mGridPanel.setAbsListView(mGridView);

    mGridView.setOnItemLongClickListener(new GridItemLongClickListener(mGamePresenter));

    if (mGridList != null) {
        onWordListComplete(mGridList);
    }

    if (mPanel1List == null) {
        mPanel1List = new ArrayList<>();
    }

    if (mPanel2List == null) {
        mPanel2List = new ArrayList<>();
    }

    if (mPanel3List == null) {
        mPanel3List = new ArrayList<>();
    }

    if (mPanel4List == null) {
        mPanel4List = new ArrayList<>();
    }

    mItemListAdapter1 = new ItemListAdapter(getContext(), mPanel1List);
    mItemListAdapter2 = new ItemListAdapter(getContext(), mPanel2List);
    mItemListAdapter3 = new ItemListAdapter(getContext(), mPanel3List);
    mItemListAdapter4 = new ItemListAdapter(getContext(), mPanel4List);

    mListView1.setAdapter(mItemListAdapter1);
    mListView1.setOnItemLongClickListener(new ListItemLongClickListener());
    mListView2.setAdapter(mItemListAdapter2);
    mListView2.setOnItemLongClickListener(new ListItemLongClickListener());
    mListView3.setAdapter(mItemListAdapter3);
    mListView3.setOnItemLongClickListener(new ListItemLongClickListener());
    mListView4.setAdapter(mItemListAdapter4);
    mListView4.setOnItemLongClickListener(new ListItemLongClickListener());
}

返回片段实例之前的屏幕截图:

Screenshot before returning to fragment instance

返回片段实例后的屏幕截图:

Screenshot after returning to fragment instance

1 个答案:

答案 0 :(得分:0)

知道了!问题是我在getView()方法本身中找到了ID,而不是在ViewHolder类中扩展RecyclerView.ViewHolder并以这种方式获取ID:

public View getView(final int i, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

    // reuse views
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_word, null);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    if (i <= mWordCards.size()-1){
        viewHolder.text.setText(mWordCards.get(i).getWord());
        convertView.setOnDragListener(new ItemDragListener(mWordCards.get(i)));
    }

    return convertView;
}

private class ViewHolder extends RecyclerView.ViewHolder {
    TextView text;
    ImageView mCloseBtn;

    public ViewHolder(View itemView) {
        super(itemView);
        text = (TextView) itemView.findViewById(R.id.word_text);
        mCloseBtn = (ImageView)itemView.findViewById(R.id.close_btn);
    }