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

标签 java android arraylist android-recyclerview

我正在尝试使用 RecyclerView 以列表格式列出不同的文本和图像,目前它为所有 5 行显示相同的文本和图像,如何使用相同的布局文件在每一行中列出不同的文本和图像和适配器类,或者我必须为每一行创建一个新的布局+数据列表+适配器类,我对java有点困惑。

主要 Activity

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
        ));
    }

最佳答案

showing the same text and images for all 5 rows

因为这里:

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 循环中使用 strItemintImageIds 数组创建 DataList 类的对象:

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

关于java - 使用 ArrayList 和 RecyclerView 列出不同的文本和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29204772/

相关文章:

java - maven 构建失败 - 无法解决 maven 依赖关系

python - os.listdir() 使用什么方法获取目录中的文件列表?

java - 如何解决无法连接到/192.168.15.186(端口80): connect failed: ETIMEDOUT (Connection timed out) in Windows Firewall

android - 如何消除android中两个textview之间的差距

android - android studio中的光标选择矩形区域

android - 我的应用程序在后台运行时被 android 系统杀死

java - 处理语言: Get inner array values from ArrayList()

java - 将 double 组列表转换为整数数组列表

java - 使用jpa映射多对多关系和可嵌入类

java - 根据输入文件创建对象