我一直在嘗試這個項目在link。一切工作正常。我也有一個輸出。我想提高它,但我不知道該怎麼做。我有一個背景,我需要將它作爲recyclerview中gridlayoutmanager的每一行的背景。 請幫助我。並在此先感謝GridLayoutManager每行相同的背景
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Context context;
RecyclerView.Adapter recyclerView_Adapter;
RecyclerView.LayoutManager recyclerViewLayoutManager;
String[] numbers = {
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
recyclerViewLayoutManager = new GridLayoutManager(context, 2);
recyclerView.setLayoutManager(recyclerViewLayoutManager);
recyclerView_Adapter = new RecyclerViewAdapter(context,numbers);
recyclerView.setAdapter(recyclerView_Adapter);
}
}
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.
Adapter<RecyclerViewAdapter.ViewHolder>{
String[] values;
Context context1;
public RecyclerViewAdapter(Context context2,String[] values2){
values = values2;
context1 = context2;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder(View v){
super(v);
textView = (TextView) v.findViewById(R.id.textview1);
}
}
@Override
public RecyclerViewAdapter.ViewHolder
onCreateViewHolder(ViewGroup parent, int viewType){
View view1 = LayoutInflater.from(context1).inflate
(R.layout.recycler_view_items,parent,false);
ViewHolder viewHolder1 = new ViewHolder(view1);
return viewHolder1;
}
@Override
public void onBindViewHolder(ViewHolder Vholder, int position){
Vholder.textView.setText(values[position]);
Vholder.textView.setBackgroundColor(Color.CYAN);
Vholder.textView.setTextColor(Color.BLUE);
}
@Override
public int getItemCount(){
return values.length;
}
}
份額更多的代碼 - 你的背景,你的適配器,不要指望我們檢查一個項目形式的問題中的鏈接,以幫助您 –
因此,當您在recycler_view_items上設置背景時,什麼都沒有發生? –
該背景將顯示在每個項目,但我需要每個行的背景。 –