我正在使用RecyclerView,但是当在RecyclerView中查看项目时,只有列表中的第一个项目被显示。
这是RecyclerView适配器: package com.example.abdelmagied.myapplication;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
/**
* Created by AbdELMagied on 7/19/2017.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{
private ArrayList<items> myitems;
public LayoutInflater inflater;
public RecyclerAdapter(Context context, ArrayList<items> myitems) {
this.myitems = myitems;
this.inflater = LayoutInflater.from(context);
}
@Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.recyclerviewrow , parent , false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position) {
holder.name.setText(myitems.get(position).name);
holder.price.setText(myitems.get(position).price);
}
@Override
public int getItemCount() {
return myitems.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView name;
TextView price;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.recyclerviewname);
price = (TextView)itemView.findViewById(R.id.recyclerviewprice);
}
}
}
以下是名为recyclerviewrow.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">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/recyclerviewname" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/recyclerviewprice" />
</LinearLayout>
以下是 Main3Activity.xml 的内容:
package com.example.abdelmagied.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import java.util.ArrayList;
public class Main3Activity extends AppCompatActivity {
public RecyclerView mRecyclerView;
public RecyclerView.LayoutManager mymanager;
public RecyclerView.Adapter recyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Bundle bundle = getIntent().getExtras();
ArrayList<items> myitems = bundle.getParcelableArrayList("myarray");
ArrayList<items> go = new ArrayList<items>();
go.add(new items("mobile" , " blablabl" , "655"));
go.add(new items("labtop" , " blablabl" , "655"));
go.add(new items("sony" , "blablbl" , "655"));
go.add(new items("mobile" , " blablabla" , "655"));
go.add(new items("mobile" , "blablabla" , "655"));
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerId);
mymanager = new LinearLayoutManager(this);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mymanager);
recyclerViewAdapter = new RecyclerAdapter(this , go);
mRecyclerView.setAdapter(recyclerViewAdapter);
}
}