回收器查看器中未列出所有项目

时间:2018-10-27 15:12:36

标签: android android-recyclerview android-adapter

我创建了显示项目列表的Recycler视图,但是,如果我向下拖动列表,则随机显示前1到10个项目,则仅显示前10名,

1

2

3

4

5

6

7

8

9

10

11

12

13

....

但是

1

2

3

4

5

6

7

8

9

10(到这里为止很好)

5

3

6 ....(仅重复1〜10个数字)

我附上了代码,有人可以给我建议吗?

private RecyclerView mItemRecyclerView;
private LinearLayoutManager mLayoutManager;
private ItemAdapter mAdapter;
private TextView mTitleTextView;
private TextView mCostTextView;
private Button mBuyButton;
private Button mSellButton;
private Button mUse;
private Button mLeaveButton;
private GameData gmd;


@Override
public void onCreate(Bundle saveInstanceState)
{
    super.onCreate(saveInstanceState);
    setContentView(R.layout.recycler_view);

    gmd = (GameData) getIntent().getSerializableExtra("gamedataToMarket");

    mItemRecyclerView = findViewById(R.id.item_recycler_view);
    mLayoutManager = new LinearLayoutManager(this);
    mItemRecyclerView.setLayoutManager(mLayoutManager);


    List<Item> items = gmd.getItems();

    mAdapter = new ItemAdapter(items);
    mItemRecyclerView.setAdapter(mAdapter);


    mLeaveButton =  findViewById(R.id.leave_Button);
    mLeaveButton.setText(R.string.leave);
    mLeaveButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {

            Intent rtn = new Intent();
            rtn.putExtra("gameDataReturnFromMarKet",gmd);
            setResult(1,rtn);
            Toast.makeText(marketActivity.this, "back to navigation from market", Toast.LENGTH_SHORT).show();
            finish();
        }
    });

    FragmentManager fm2 = getSupportFragmentManager();
    Fragment fragment2 = fm2.findFragmentById(R.id.status_recycler);
    if(fragment2 == null){
        fragment2 = new status_fragment();
        fm2.beginTransaction()
                .add(R.id.status_recycler,fragment2)
                .commit();
    }




}

private class ItemHolder extends RecyclerView.ViewHolder{
    private Item mItems;

    public ItemHolder(View view) {
        super(view);

        mTitleTextView = (TextView) itemView.findViewById(R.id.item_name);
        mCostTextView = (TextView) itemView.findViewById(R.id.item_cost);
        mBuyButton = (Button) itemView.findViewById(R.id.button_1_option);
        mBuyButton.setText(R.string.bought);
        mBuyButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Toast.makeText(marketActivity.this, R.string.bought, Toast.LENGTH_SHORT).show();
            }
        });
        mSellButton = (Button) itemView.findViewById(R.id.button_2_option);
        mSellButton.setText(R.string.sold);
        mSellButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Toast.makeText(marketActivity.this, R.string.sold, Toast.LENGTH_SHORT).show();        // function for sell item
            }
        });
        mUse = (Button) itemView.findViewById(R.id.button_3_option);
        mUse.setText(R.string.use);
        mUse.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Toast.makeText(marketActivity.this, R.string.use, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void bind(Item item) {
        mItems = item;
        mTitleTextView.setText(item.getDescription());
        mCostTextView.setText(String.valueOf(item.getValue()));
    }
}

private class ItemAdapter extends RecyclerView.Adapter<ItemHolder> {
    private List<Item> items;

    public ItemAdapter(List<Item> item) {
        items = item;
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.holdingview,parent,false);

        return new ItemHolder(v);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, int position) {
        holder.bind(items.get(position));
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}

}

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="0.8"
android:padding="1dp"
android:text="item name" />

<TextView
android:id="@+id/item_cost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:padding="1dp"
android:text="cost" />
<Button
android:id="@+id/button_1_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/button_2_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/button_3_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您必须像这样对代码进行一些更改:

  1. 在ItemHolder类中,您以超级方法传递“视图”,但使用id通过id获取 “ itemView”。您必须使用与您相同的参数来查找视图的ID 通过超级方法。 因此,像这样更改所有的findViewById:

    mTitleTextView = (TextView) view.findViewById(R.id.item_name);
    mCostTextView = (TextView) view.findViewById(R.id.item_cost);
    
  2. 您在以下位置声明视图(用于填充数据的TextView和Button) 活动并在viewHolder中获取ID。您必须在viewholder中声明它们。
    您的情况是这样的:

    private class ItemHolder extends RecyclerView.ViewHolder {
    private TextView mTitleTextView;
    private TextView mCostTextView;
    private Button mBuyButton;
    private Button mSellButton;
    private Button mUse;
    
    public ItemHolder(View view) {
        super(view);
        mTitleTextView = (TextView) view.findViewById(R.id.item_name);
        mCostTextView = (TextView) view.findViewById(R.id.item_cost);
        mBuyButton = (Button) view.findViewById(R.id.button_1_option);
        mSellButton = (Button) view.findViewById(R.id.button_2_option);
        mUse = (Button) view.findViewById(R.id.button_3_option);
    }
    }