今天要做的是在Fragment內加入RecyclerView。
在我的觀察下發現了在很多手機軟體中都有滑動的欄位,可以是清單欄位、設定面板等等的畫面上都可能是使用RecyclerView這個功能,而附加的不是單純的按鈕點擊更換頁面,而是使用滑動更換頁面的方式來操作,這個滑動更換就像是ViewPager的功能。
在原先已經創建完成的Fragment下新增onViewCreated,用來初始化物件。(官方onViewCreated敘述)
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//前面已經有將View定義出來了後面所設定的範例會如下
recyclerView = view.findViewById(R.id.Shopmainview);
//這行的context是在最前面先定義一個Context。
recyclerView.setLayoutManager(new LinearLayoutManager (context));
//以下的設定與在Activity的設定類似
listAdapter = new ListAdapter ();
recyclerView.setAdapter(listAdapter);
listAdapter.makedata();
}
Context引用Android Studio官方的述說。
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
因為在Fragment內無法使用this來指定到畫面,所以使用Context進行設定。
public class BlankFragment_Scene02 extends Fragment {
private Context context;
RecyclerView recyclerView;
ListAdapter listAdapter;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public BlankFragment_Scene02 () {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment_Scene02.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment_Scene02 newInstance (String param1, String param2) {
BlankFragment_Scene02 fragment = new BlankFragment_Scene02 ();
Bundle args = new Bundle ();
args.putString (ARG_PARAM1, param1);
args.putString (ARG_PARAM2, param2);
fragment.setArguments (args);
return fragment;
}
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
if (getArguments () != null) {
mParam1 = getArguments ().getString (ARG_PARAM1);
mParam2 = getArguments ().getString (ARG_PARAM2);
}
}
///Fragment要抓到Activity的用法
// 要先找到建立這個Fragment的Activity
// 接著建立View來指定到Fragment
// */
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate (R.layout.fragment_blank__scene02, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView title = view.findViewById(R.id.title);
recyclerView = view.findViewById(R.id.Shopmainview);
recyclerView.setLayoutManager(new LinearLayoutManager (context));
listAdapter = new ListAdapter ();
recyclerView.setAdapter(listAdapter);
listAdapter.makedata();
}
}
R.layout.(自行創立的XML名稱)
比較需要更改。 @NonNull
@Override
public ListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list,parent,false);
return new ViewHolder(view);
}
這邊不太需要改動,只要自己在前面有先設定完成RecyclerView可以直接拿到Fragment使用,也就是在上方程式碼的listAdapter.makedata();
直接將Adapter所建立的假資料拿進來就好,這邊我直接貼Adapter全部的程式碼。
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
ArrayList<HashMap<String ,String>> arrayList = new ArrayList<>();
class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvname,tvdate,tvtime,tvrewardpoint;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tvname = itemView.findViewById(R.id.textView);
tvdate = itemView.findViewById(R.id.textView2);
tvtime = itemView.findViewById(R.id.textView3);
tvrewardpoint = itemView.findViewById(R.id.textView4);
}
public void itemView(HashMap<String, String> stringStringHashMap) {
}
}
@NonNull
@Override
public ListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListAdapter.ViewHolder holder, int position) {
holder.tvname.setText(arrayList.get(position).get("name_infomation"));
holder.tvdate.setText("YYYY/MM/DD");
holder.tvtime.setText("hh/mm");
holder.tvrewardpoint.setText(arrayList.get(position).get("rewardpoint_infomation"));
}
@Override
public int getItemCount() {
return arrayList.size();
}
public void makedata(){
for (int i = 0;i<100;i++){
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("name_infomation","商店代號:"+String.format("%02d",i+1));
hashMap.put("rewardpoint_infomation",String.format ("%02d",i*2));
arrayList.add(hashMap);
}
}
}
設定畫面
的RecyclerView滑動,我是將文字放入設定的String陣列中,並且使用for迴圈將資料依序讀取並放入hashMap中並且取得的方式不做改動,只是我是以用相同的方式在另外一個fragment上建立這個假資料的。 public void makesettinglist(){
String settinglist[] = {"我的帳號","主題設定","付款方式","地址設定","設定","使用者規範","登出"};
for (int i = 0;i<settinglist.length;i++){
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("name_infomation",String.valueOf(settinglist[i]));
arrayList.add(hashMap);
}
}