使用ArrayList和RecyclerView列出不同的文本和图像

时间:2015-03-23 07:02:26

标签: java android arraylist android-recyclerview

我正在尝试使用RecyclerView以列表格式列出不同的文本和图像,此时它显示所有5行的相同文本和图像如何使用相同的布局文件列出每行中的不同文本和图像和适配器类,或者我必须为每个我不熟悉的Java行创建一个新的布局+ datalist + adapter类。

主要活动

public class MainActivity extends ActionBarActivity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter (see also next example)
    ArrayList<DataList> dataList = new ArrayList<DataList>();

    for (int i = 0; i < 5; i ++ ) {

        dataList.add(new DataList(

                "Ball Juggle (while standing)",
                "Hold",
                R.drawable.lt1,
                "+",
                "Tap",
                R.drawable.lt2
        ));
    }

    mAdapter = new MyAdapter(dataList);
    mRecyclerView.setAdapter(mAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

我的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<DataList> dataList;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public View view;
    public ViewHolder(View v) {
        super(v);
        view = v;
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<DataList> dataList) {
    this.dataList = dataList;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_layout, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
   TextView title = (TextView) holder.view.findViewById(R.id.title);
    TextView desc = (TextView) holder.view.findViewById(R.id.desc);
    ImageView imageView = (ImageView) holder.view.findViewById(R.id.imageView);
    TextView desc2 = (TextView) holder.view.findViewById(R.id.desc2);
    TextView desc3 = (TextView) holder.view.findViewById(R.id.desc3);
    ImageView imageView2 = (ImageView) holder.view.findViewById(R.id.imageView2);


    title.setText(dataList.get(position).getTitle());
    desc.setText(dataList.get(position).getDesc());
    imageView.setImageResource(dataList.get(position).getImage());
    desc2.setText(dataList.get(position).getDesc2());
    desc3.setText(dataList.get(position).getDesc3());
    imageView2.setImageResource(dataList.get(position).getImage2());


}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return dataList.size();
}
}

数据列表

public class DataList {

String title;
String desc;
int image;
String desc2;
String desc3;
int image2;

public DataList(String title, String desc, int image, String desc2, String desc3, int image2) {
    this.title = title;
    this.desc = desc;
    this.image = image;
    this.desc2 = desc2;
    this.desc3 = desc3;
    this.image2 = image2;
}

public String getTitle() {
    return title;
}

public String getDesc() {
    return desc;
}

public int getImage() {
    return image;
}

public String getDesc2 () {
    return desc2;
}

public String getDesc3 () {
    return desc3;
}

public int getImage2 () {
    return image2;
}
}

行布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:paddingRight="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/title"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:paddingLeft="5dp"
    android:paddingRight="2dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/desc"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/title"
    android:layout_toEndOf="@+id/title" />

<ImageView
    android:paddingLeft="2dp"
    android:paddingRight="2dp"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/desc"
    android:layout_toEndOf="@+id/desc" />

<TextView
    android:paddingLeft="2dp"
    android:paddingRight="2dp"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/desc2"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView" />

<TextView
    android:paddingLeft="2dp"
    android:paddingRight="2dp"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/desc3"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/desc2"
    android:layout_toEndOf="@+id/desc2" />

<ImageView
    android:paddingLeft="2dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/desc3"
    android:layout_toEndOf="@+id/desc3" />


</RelativeLayout>

尝试这样做,但显然代码是错误的,因为它只显示一行我必须如何编辑此代码

ArrayList<DataList> dataList = new ArrayList<DataList>();

    for (int i = 0; i < 1; i ++ ) {

        dataList.add(new DataList(

                "France",
                "Russia",
                R.drawable.lt1,
                "America",
                "Europe",
                R.drawable.lt2
        ));
    }

    ArrayList<DataList2> dataList2 = new ArrayList<DataList2>();

    for (int i = 0; i < 1; i ++ ) {

        dataList2.add(new DataList2(

                "South Africa",
                "Brazil",
                R.drawable.lt1,
                "New Zeland",
                "Pakistan",
                R.drawable.lt2
        ));
    }

1 个答案:

答案 0 :(得分:1)

  

显示所有5行的相同文本和图像

因为这里:

dataList.add(new DataList(
                "Ball Juggle (while standing)",
                "Hold",
                R.drawable.lt1,
                "+",
                "Tap",
                R.drawable.lt2
        ));

通过在DataList类构造函数中传递相同的值来创建DataList类的所有5个对象。

使用想要在ListView行中显示的不同值来创建DataList类对象。

编辑: 这样做:

1。创建项目数组:

String[] strItem = {"Ball Juggle (while standing)","b","c","b","c"};
int[] intImageIds = {R.drawable.lt1,R.drawable.lt1,...};

2。在for循环中使用DataListstrItem数组创建intImageIds类的对象:

for (int i = 0; i < 5; i ++ ) {
    dataList.add(new DataList(
            strItem[i],
            "Hold",
            intImageIds[i],
            "+",
            "Tap",
            R.drawable.lt2
    ));
}