Как добавить изображения в моем представлении Recycler в каждой строке списка? - PullRequest
1 голос
/ 03 мая 2020

У меня проблема. Я новичок в Android. Через пару месяцев я нахожусь. Кто-то может помочь мне добавить представление изображения в этот список переработчиков. спасибо, приятель.

mainacctivity. 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="MainActivicty";
   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);
    }
}

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

        }
    }
}

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

это моя строка. xml

<?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>

помогите мне // добавить просмотр изображения в виде списка. спасибо.

1 Ответ

0 голосов
/ 03 мая 2020

Я столкнулся с подобной проблемой и успешно решил ее. Теперь я могу дать вам свой код. Я думаю, что это поможет вам

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<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 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<ItemAdapter> mList = new ArrayList<>();
    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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/solve_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_margin="30dp"
        android:background="@drawable/ic_calculator" />

    <com.example.geometry.GUI.Builder
        android:id="@+id/builder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/recycler_view"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
    />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

</RelativeLayout>

item_custom. java

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


</RelativeLayout>
...