Android Recycler View显示默认值?物品清单

时间:2017-05-26 08:40:11

标签: android android-layout android-recyclerview

我目前正在Android应用程序上工作,无法弄清楚为什么回收者视图显示如下:项目0项目1 ...项目9

我的主要活动:

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

private RecyclerView recView;
private DerpAdapter adapter;
private List<ListItem>  itemList = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MakeList();

    recView = (RecyclerView)findViewById(R.id.rec_list);
    recView.setLayoutManager(new LinearLayoutManager(this));
    recView.setHasFixedSize(true);

    adapter = new DerpAdapter(itemList, this);
    recView.setAdapter(adapter);


}

private void MakeList()
{
    ListItem item = new ListItem("1", "2");
    itemList.add(item);

    item = new ListItem("2", "3");
    itemList.add(item);

    item = new ListItem("3", "2");
    itemList.add(item);

    item = new ListItem("4", "1");
    itemList.add(item);

    //adapter.notifyDataSetChanged();
} }

我的适配器:

public class DerpAdapter extends RecyclerView.Adapter<DerpAdapter.DerpHolder> {

private List<ListItem> listData;
private LayoutInflater inflater;

public DerpAdapter(List<ListItem> listData, Context c){
    inflater = LayoutInflater.from(c);
    this.listData = listData;
}

@Override
public DerpHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.list_item, parent, false);
    return new DerpHolder(view);
}

@Override
public void onBindViewHolder(DerpHolder holder, int position) {
    ListItem item = listData.get(position);
    holder.counter.setText(item.getCount());
    holder.circle.setText(item.getNumber());
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

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

public class DerpHolder extends RecyclerView.ViewHolder {

    private TextView counter;
    private TextView circle;
   // private View container;

    public DerpHolder(View itemView) {
        super(itemView);

        counter = (TextView) itemView.findViewById(R.id.counter);
        circle = (TextView) itemView.findViewById(R.id.circle);
        //container = itemView.findViewById(R.id.cont_item_root);
    }
} }

列表项的XML文件

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cont_item_root">

<TextView
    android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    android:gravity="center"
    android:paddingLeft="8dp"
    android:text="1" />

<TextView
    android:id="@+id/circle"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:tint="@color/colorPrimary"
    android:background = "@drawable/rounded_button"
    android:text="1"/> </LinearLayout>

主要活动的XML文件

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ui.MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:id="@+id/start"
    android:background = "@drawable/rounded_button"
    android:layout_alignTop="@+id/stop"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="49dp"
    android:layout_marginStart="49dp"
    android:layout_marginBottom="30dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stop"
    android:id="@+id/stop"
    android:background = "@drawable/rounded_button"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/start"
    android:layout_toEndOf="@+id/start"
    android:layout_marginLeft="56dp"
    android:layout_marginStart="56dp" />

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/rec_list"
    android:layout_below="@id/start"
    >

</android.support.v7.widget.RecyclerView> </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

你需要什么? 在这种情况下,recyclerview只有4个项目(item0到item 3),但你在第二个textview下面显示类似于一个textview,类似于第0项到第9项的显示在布局文件和文本对齐中更改,如果有任何问题,那么只需评论

答案 1 :(得分:0)

更新列表后,您需要在notifyDataSetChanged上致电RecyclerView.Adapter。 此方法告诉适配器使用新数据结构更新视图内容。