не все элементы перечислены в программе просмотра Recycler - PullRequest
0 голосов
/ 27 октября 2018

я создал вид Recycler, который показывает список элементов, однако, отображаются только первые 10 лучших, если я перетаскиваю список, повторяю случайным образом первые 1 ~ 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 голосов
/ 28 октября 2018

Вы должны внести некоторые изменения в свой код, например:

  1. В классе ItemHolder вы передаете "view" в методе super, но получаете по id с помощью "itemView". Вы должны найти идентификатор представления, используя тот же параметр, который вы переходя в супер метод. так что измените все ваши findViewById так:

    mTitleTextView = (TextView) view.findViewById(R.id.item_name);
    mCostTextView = (TextView) view.findViewById(R.id.item_cost);
    
  2. Вы объявляете представления (TextView и Button, которые вы используете для заполнения данных) в активность и получение идентификатора в 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);
    }
    }
    
...