我是Android开发的新手,但我正在尝试使用RecyclerView构建一个简单的应用程序。我已经按照几个不同教程的说明进行操作,但每当我启动应用程序时,我得到的只是一个空白屏幕。关于我可能出错的任何想法?我错过了一些代码吗?提前谢谢。
这是我的RecyclerViewAdapter:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.Collections;
import java.util.List;
public class Recycler_View_Adapter extends RecyclerView.Adapter<View_Holder> {
List<Data> list = Collections.emptyList();
Context context;
@Override
public View_Holder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
View_Holder holder = new View_Holder(v);
return holder;
}
@Override
public void onBindViewHolder(View_Holder holder, int position) {
//Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
holder.title.setText(list.get(position).title);
holder.description.setText(list.get(position).description);
holder.imageView.setImageResource(list.get(position).imageId);
//animate(holder);
}
@Override
public int getItemCount() {
//returns the number of elements the RecyclerView will display
return list.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
// Insert a new item to the RecyclerView on a predefined position
public void insert(int position, Data data) {
list.add(position, data);
notifyItemInserted(position);
}
// Remove a RecyclerView item containing a specified Data object
public void remove(Data data) {
int position = list.indexOf(data);
list.remove(position);
notifyItemRemoved(position);
}
}
以下是我的MainActivity的代码
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private RecyclerView mRecyclerView;
private List<Data> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Data> data = fill_with_data();
Recycler_View_Adapter adapter = new Recycler_View_Adapter();
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public List<Data> fill_with_data() {
List<Data> data = new ArrayList<>();
data.add(new Data("FTWT item", "ftwt item description ", R.drawable.ftwt1));
data.add(new Data("Ftwt item 2", "ftwt item2 description ", R.drawable.ftwt2));
data.add(new Data("ftwt merch", "ftwt merch description", R.drawable.ftwt_merch1));
data.add(new Data("luca item 1", "luca item1 description", R.drawable.luca1));
data.add(new Data("luca merch1", "luca merch1 description", R.drawable.luca_shirt1));
data.add(new Data("luca merch2", "luca merch2 item description ", R.drawable.luca_shirt2));
return data;
}
}
答案 0 :(得分:1)
您似乎没有将data
传递给您的适配器。
更改此
Recycler_View_Adapter adapter = new Recycler_View_Adapter();
到
Recycler_View_Adapter adapter = new Recycler_View_Adapter(data);
您的Recycler_View_Adapter
list
现在总是空着。添加构造函数以接受数据:
public Recycler_View_Adapter(List<Data> data){
list = data;
}