如何在我的RecyclerView中的每行列表中添加图片?

huangapple go评论84阅读模式
英文:

How to add images in my Recycler view with in every line of list?

问题

我是新手Android开发者,遇到了在RecyclerView列表中添加ImageView的问题。我已经努力解决这个问题几个月了,但仍然找不到解决方案。

有没有人可以帮助我解决这个问题?以下是我的MainActivity.java文件供参考:

package com.example.customlistview;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

   private static final String TAG = "MainActivity";
   private Context mContext;
   ArrayList<String> titleArrayList;
   RecyclerView mRecyclerView;
    int images[] = {R.drawable.facebook, R.drawable.whatsapp, R.drawable.twitter, R.drawable.instagram, R.drawable.youtube};

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

        titleArrayList = new ArrayList<String>();
        titleArrayList.add(Constants.NUMBER_SYSTEM);
        titleArrayList.add(Constants.POWER_ICDICES);
        titleArrayList.add(Constants.SIMPLIFICATION);
        titleArrayList.add(Constants.ALGEBRA);

        mContext = MainActivity.this;
        mRecyclerView = (RecyclerView) findViewById(R.id.Recycle);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

        TitleAdapter adapter = new TitleAdapter(mContext, titleArrayList, new CustomItemClickListener() {
            @Override
            public void onItemClick(View v, int i) {
                Toast.makeText(mContext, "clicked" + titleArrayList.get(i), Toast.LENGTH_SHORT).show();
            }
        });
        mRecyclerView.setAdapter(adapter);
    }
}

TitleAdapter.java

package com.example.customlistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class TitleAdapter extends RecyclerView.Adapter<TitleAdapter.MyViewHolder> {
    private Context mContext;
    private ArrayList<String> titleList;
    private CustomItemClickListener clickListener;

    public TitleAdapter(Context mContext, ArrayList<String> titleList, CustomItemClickListener clickListener) {
        this.mContext = mContext;
        this.titleList = titleList;
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
        final MyViewHolder viewHolder = new MyViewHolder(view);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.onItemClick(view, viewHolder.getPosition());
            }
        });

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.titletext.setText(titleList.get(position).replace("_", " "));
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView titletext;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            titletext = (TextView) itemView.findViewById(R.id.textView1);
        }
    }
}

Constants.java

package com.example.customlistview;

class Constants {
    static final String NUMBER_SYSTEM = "NUMBER_SYSTEM(संख्या पद्धति)";
    static final String POWER_ICDICES = "POWER,ICDICES & SURDS";
    static final String SIMPLIFICATION = "SIMPLIFICATION(संख्या पद्धति)";
    static final String ALGEBRA = "ALGEBRA(बीजगणित)";
}

CustomItemClickListener.java

package com.example.customlistview;

import android.view.View;

public interface CustomItemClickListener {
    public void onItemClick(View v, int i);
}

这是我的row.xml文件,包含ImageView:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:contentPadding="5dp"
    app:cardElevation="3dp"
    app:cardCornerRadius="10dp"
    app:cardUseCompatPadding="true"
    app:cardBackgroundColor="@color/whiteColor">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">

        <ImageView
            android:id="@+id/image"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/whatsapp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:orientation="vertical"
            android:layout_marginLeft="5dp">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:text="@string/main_title"
                android:textColor="@color/blackColor"
                android:textSize="15sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

希望这些代码能帮助你解决在RecyclerView列表中添加ImageView的问题。如果有任何其他问题,请随时提出。

英文:

I am new to Android development and am having trouble adding an ImageView to a RecyclerView list. I have been working on this for a couple of months, but am still struggling to find a solution.

Is there anyone who could help me with this issue? Here is my MainActivity.java file for reference:

package com.example.customlistview;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG=&quot;MainActivicty&quot;;
private Context mcontext;
ArrayList&lt;String&gt; titleArrayList;
RecyclerView mRecyclerview;
int images[] = {R.drawable.facebook, R.drawable.whatsapp, R.drawable.twitter, R.drawable.instagram, R.drawable.youtube};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleArrayList=new ArrayList&lt;String&gt;();
titleArrayList.add(Constants.NUMBER_SYSTEM);
titleArrayList.add(Constants.POWER_ICDICES);
titleArrayList.add(Constants.SIMPLIFICATION);
titleArrayList.add(Constants.ALGEBRA);
;
mcontext=MainActivity.this;
mRecyclerview=(RecyclerView) findViewById(R.id.Recycle);
mRecyclerview.setHasFixedSize(true);
mRecyclerview.setItemAnimator(new DefaultItemAnimator());
mRecyclerview.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
TitleAdapter  adapter=new TitleAdapter(mcontext, titleArrayList, new CustomItemClickListener() {
@Override
public void onItemClick(View v, int i) {
Toast.makeText(mcontext, &quot;clicked&quot;+titleArrayList.get(i),Toast.LENGTH_SHORT).show();
}
});
mRecyclerview.setAdapter(adapter);
}
}

TitleApdapter.java

package com.example.customlistview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class TitleAdapter extends RecyclerView.Adapter&lt;TitleAdapter.MyViewHolder&gt;  {
private Context mcontext;
private ArrayList&lt;String&gt; titleList;
private CustomItemClickListener clickListener;
public TitleAdapter(Context mcontext, ArrayList&lt;String&gt; titleList, CustomItemClickListener clickListener) {
this.mcontext = mcontext;
this.titleList = titleList;
this.clickListener = clickListener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View view= LayoutInflater.from(mcontext).inflate(R.layout.row,parent,false);
final MyViewHolder viewHolder=new MyViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListener.onItemClick(view,viewHolder.getPosition());
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.titletext.setText(titleList.get(position).replace(&quot;_&quot;,&quot; &quot;));
}
@Override
public int getItemCount() {
return titleList.size();
}
public  class MyViewHolder extends RecyclerView.ViewHolder{
TextView titletext;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titletext=(TextView) itemView.findViewById(R.id.textView1);
}
}
}

Constant.java

package com.example.customlistview;
class Constants {
static final String NUMBER_SYSTEM=&quot;NUMBER_SYSTEM(संख्या पद्धति)&quot;;
static final String POWER_ICDICES=&quot;POWER,ICDICES &amp; SURDS&quot;;
static final String SIMPLIFICATION=&quot;SIMPLIFICATION(संख्या पद्धति)&quot;;
static final String ALGEBRA=&quot;ALGEBRA(बीजगणित)&quot;;
}

CustomItemClickListener.java

package com.example.customlistview;
import android.view.View;
public interface CustomItemClickListener {
public void onItemClick(View v,int i);
}

this is my row.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.cardview.widget.CardView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
app:contentPadding=&quot;5dp&quot;
app:cardElevation=&quot;3dp&quot;
app:cardCornerRadius=&quot;10dp&quot;
app:cardUseCompatPadding=&quot;true&quot;
app:cardBackgroundColor=&quot;@color/whiteColor&quot;
&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;horizontal&quot;
android:padding=&quot;10dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/image&quot;
android:layout_width=&quot;40dp&quot;
android:layout_height=&quot;40dp&quot;
android:src=&quot;@drawable/whatsapp&quot; /&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;5dp&quot;
android:orientation=&quot;vertical&quot;
android:layout_marginLeft=&quot;5dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textView1&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;2dp&quot;
android:text=&quot;@string/main_title&quot;
android:textColor=&quot;@color/blackColor&quot;
android:textSize=&quot;15sp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;

Any guidance would be greatly appreciated. Thank you in advance.

答案1

得分: 0

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.example.geometry.R;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ItemAdapter> mList;
    private Context mContext;

    public ListAdapter(List<ItemAdapter> list, Context context){
        super();
        mList = list;
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_custom, parent, false);

        final ViewHolder viewHolder = new ViewHolder(v);

        viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Input mode = " + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
                Builder.mode = viewHolder.getAdapterPosition();
            }
        });

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ItemAdapter itemAdapter = mList.get(position);
        ((ViewHolder) viewHolder).mImg.setImageResource(itemAdapter.getImage());
    }

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

    class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView mImg;

        public ViewHolder(View itemView) {
            super(itemView);
            mImg = itemView.findViewById(R.id.img_item);
        }
    }
}

public class ItemAdapter {
    private int image;

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }
}

import android.os.Bundle;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecycleView;
    private List<ItemAdapter> mList = new ArrayList<>();
    private ListAdapter mAdapter;

    ImageButton solve_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        addList();
        adapter();
    }

    private void init(){
        setContentView(R.layout.activity_main);
        mRecycleView = findViewById(R.id.recycler_view);
    }

    private void addList(){
        ItemAdapter itemAdapter = new ItemAdapter();
        itemAdapter.setImage(R.drawable.circle);
        mList.add(itemAdapter);

        // ... (similar lines for adding more items)

    }

    private void adapter(){
        mAdapter = new ListAdapter(mList, this);
        mRecycleView.setAdapter(mAdapter);
        mRecycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        mAdapter.notifyDataSetChanged();
    }
}

Please note that the XML content was not included as you requested only the translated code. If you have any further questions or need clarifications, feel free to ask.

英文:

I came across a similar problem and successfully solved it. Now I can give you my code. I think it will help you

ListAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import com.example.geometry.R;
import java.util.List;
public class ListAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; {
private List&lt;ItemAdapter&gt; mList;
private Context mContext;
public ListAdapter(List&lt;ItemAdapter&gt; list, Context context){
super();
mList = list;
mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View v = LayoutInflater.from(mContext).inflate(R.layout.item_custom, parent, false);
final ViewHolder viewHolder = new ViewHolder(v);
viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), &quot;Input mode = &quot; + viewHolder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
Builder.mode = viewHolder.getAdapterPosition();
}
});
return viewHolder;
}
@Override
public void onBindViewHolder( RecyclerView.ViewHolder viewHolder, int position) {
ItemAdapter itemAdapter = mList.get(position);
((ViewHolder) viewHolder).mImg.setImageResource(itemAdapter.getImage());
}
@Override
public int getItemCount() {
return mList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView mTv_name;
public ImageView mImg;
public ViewHolder(View itemView) {
super(itemView);
mImg = (ImageView) itemView.findViewById(R.id.img_item);
}
}
}

ItemAdapter.java

public class ItemAdapter {
private int image;
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.geometry.GUI.Builder;
import com.example.geometry.GUI.ItemAdapter;
import com.example.geometry.GUI.ListAdapter;
import com.example.geometry.Output.SolveActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecycleView;
private List&lt;ItemAdapter&gt; mList = new ArrayList&lt;&gt;();
private ListAdapter mAdapter;
ListView listView;
RelativeLayout layout;
ImageButton solve_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
addList();
adapter();
}
private void init(){
setContentView(R.layout.activity_main);
layout = new RelativeLayout(this);
mRecycleView = findViewById(R.id.recycler_view);//new RecyclerView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mRecycleView.setLayoutParams(params);
}
private void addList(){
ItemAdapter itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.circle);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.line);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.move);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.angle);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.regular_triangle);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.right_triangle);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.square);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.trapeze);
mList.add(itemAdapter);
}
private void adapter(){
mAdapter = new ListAdapter(mList, this);
mRecycleView.setAdapter(mAdapter);
mRecycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
mAdapter.notifyDataSetChanged();
}
}

activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;ImageButton
android:id=&quot;@+id/solve_button&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentRight=&quot;true&quot;
android:layout_margin=&quot;30dp&quot;
android:background=&quot;@drawable/ic_calculator&quot; /&gt;
&lt;com.example.geometry.GUI.Builder
android:id=&quot;@+id/builder&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_above=&quot;@+id/recycler_view&quot;
/&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/recycler_view&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentBottom=&quot;true&quot;
/&gt;
&lt;EditText
android:id=&quot;@+id/editText&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:ems=&quot;10&quot;
android:inputType=&quot;textPersonName&quot;
android:text=&quot;Name&quot; /&gt;
&lt;/RelativeLayout&gt;

item_custom.java

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_margin=&quot;2dp&quot;
android:background=&quot;#fff&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
android:layout_gravity=&quot;center&quot;
android:scaleType=&quot;center&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/img_item&quot;
android:layout_width=&quot;wrap_content&quot;
android:contentDescription=&quot;@string/app_name&quot;
android:layout_height=&quot;wrap_content&quot; 
android:layout_gravity=&quot;center&quot;
android:scaleType=&quot;center&quot;
android:layout_centerInParent=&quot;true&quot;/&gt;
&lt;/RelativeLayout&gt;

huangapple
  • 本文由 发表于 2020年5月3日 15:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61570787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定