如何在android中安排RecycleView上的数据?

时间:2016-08-20 03:32:51

标签: java android android-layout listview android-studio

我想使用RecycleView来显示我的数据数组。

这是我的Adapater类,它在视图上设置数据。

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{

        private Context context;
        private List<Pojo> dataList;

        public DataAdapter(Context context, List<Pojo> dataList) {
            this.context = context;
            this.dataList = dataList;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main,parent,false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.categoryTV.setText(dataList.get(position).getGameName());
        }

        @Override
        public int getItemCount() {

            return dataList.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder{

            public TextView categoryTV;

            public MyViewHolder(View itemView) {
                super(itemView);
                categoryTV = (TextView) itemView.findViewById(R.id.text);
            }
        }
    }

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/rellayout"
    tools:context="com.acknotech.kiran.retrofit_second.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>
</RelativeLayout>

这是MainActivity ..

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private DataAdapter dataAdapter;
    private List<Pojo> dataArrayList;

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

    private void initViews(){
        recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        loadJSON();
    }

    private void loadJSON(){
        dataArrayList = new ArrayList<>();
        Retrofit retrofit=new Retrofit.Builder().baseUrl("my url").addConverterFactory(GsonConverterFactory.create()).build();
        AllGamesAPI requestInterface=retrofit.create(AllGamesAPI.class);

        Call<List<Pojo>> call= requestInterface.getJSON();
        call.enqueue(new Callback<List<Pojo>>() {
            @Override
            public void onResponse(Call<List<Pojo>> call, Response<List<Pojo>> response) {
                dataArrayList = response.body();
                dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
                recyclerView.setAdapter(dataAdapter);
            }

            @Override
            public void onFailure(Call<List<Pojo>> call, Throwable t) {
                    Log.e("Error",t.getMessage());
            }

        });
    }
}

当我尝试执行它时,总是一次只打印一个项目,但在dataList中我有11个项目。

如何使用RecyclerView获取页面中的所有项目?

谢谢&amp;此致

1 个答案:

答案 0 :(得分:1)

您不应该在onCreateViewHolder中夸大活动布局,而是仅为一个项目(例如android.R.layout.simple_list_item_1或自定义项目)夸大布局。此布局不应使用match_parent作为项目的高度。