刚刚实现了GridView的实现方式,领导发话,最好用recyclerView更好,又用了一下午的时间把recyclerView研究了下,具体代码如下:
FragmentTwo.java
package com.example.navigation; import android.content.Context; import android.graphics.Rect; import android.net.Uri; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.navigation.Navigation; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import java.util.ArrayList; import java.util.List; public class FragmentTwo extends Fragment { private View mRootView; private RecyclerView recyclerView; private PlayListAdapter adapter; private List<Playlist_Info> list = new ArrayList<>(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment mRootView = inflater.inflate(R.layout.fragment_two, container, false); recyclerView = mRootView.findViewById(R.id.recycler_view); GridLayoutManager layoutManager = new GridLayoutManager(getContext(),4,GridLayoutManager.VERTICAL,false); recyclerView.addItemDecoration(new SpacesItemDecoration(new Rect(40,0,0,64))); recyclerView.setLayoutManager(layoutManager); initlist(); adapter = new PlayListAdapter(getContext(),list); recyclerView.setAdapter(adapter); return mRootView; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Button fragment_one_back = view.findViewById(R.id.btn_go_back_fragment_one); fragment_one_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Navigation.findNavController(view).navigate(R.id.action_fragmentTwo_to_fragmentOne); } }); Button btn_activity2 = view.findViewById(R.id.btn_go_activity2); btn_activity2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Navigation.findNavController(view).navigate(R.id.action_fragmentTwo_to_main2Activity); } }); } @Override public void onDestroy() { super.onDestroy(); } public void initView() { recyclerView = mRootView.findViewById(R.id.recycler_view); GridLayoutManager layoutManager = new GridLayoutManager(getContext(),5,GridLayoutManager.VERTICAL,false); recyclerView.setLayoutManager(layoutManager); initlist(); adapter = new PlayListAdapter(getContext(),list); recyclerView.setAdapter(adapter); } public void initlist() { for(int i = 1; i < 21;i ) { if(2 == i) { list.add(new Playlist_Info("大江东区浪淘尽,千古风流人物","3:22",30)); continue; } else { list.add(new Playlist_Info("精绝古城第" String.valueOf(i) "集","3:22",30)); } } } }
PlayListAdapter.java
package com.example.navigation; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.PlayListHoler> { private String log_tag = "GridViewAdapter"; private Context context; private List<Playlist_Info> listitem; static class PlayListHoler extends RecyclerView.ViewHolder{ TextView textview; TextView textview_time; AudioColumnView audioColumnView; ProgressBar pb_progressbar; ProgressBar pb_listening_progressbar; public PlayListHoler(View view) { super(view); pb_progressbar = view.findViewById(R.id.pb_progressbar); pb_listening_progressbar=view.findViewById(R.id.pb_listening_progressbar); textview = (TextView)view.findViewById(R.id.textView); textview_time = (TextView)view.findViewById(R.id.textView_time); audioColumnView = (AudioColumnView)view.findViewById(R.id.acv); } } public PlayListAdapter(Context context,List<Playlist_Info> listitem){ this.context = context; this.listitem = listitem; } @NonNull @Override public PlayListHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.playlist_item,parent,false); return new PlayListHoler(view); } @Override public void onBindViewHolder(@NonNull PlayListHoler holder, int position) { Playlist_Info info = listitem.get(position); if(info != null) { int text_left_margin = 0; if(position == 0) { text_left_margin = 19; holder.pb_progressbar.setProgress(38); holder.pb_listening_progressbar.setVisibility(View.GONE); holder.audioColumnView.setVisibility(View.GONE); holder.textview_time.setText("已听38%"); } else if(position == 1) { text_left_margin = 67; holder.pb_listening_progressbar.setProgress(listitem.get(position).get_item_progress()); holder.pb_progressbar.setVisibility(View.GONE); holder.pb_listening_progressbar.setVisibility(View.VISIBLE); holder.audioColumnView.setVisibility(View.VISIBLE); holder.textview_time.setText("已听38%"); } else { text_left_margin = 26; holder.pb_progressbar.setProgress(0); holder.pb_progressbar.setVisibility(View.VISIBLE); holder.pb_listening_progressbar.setVisibility(View.GONE); holder.audioColumnView.setVisibility(View.GONE); holder.textview_time.setText( info.get_item_time()); } Log.d(log_tag, String.valueOf(info.get_item_name().length())); //改变字体位置 ViewGroup.MarginLayoutParams marginLayoutParams_text = new ViewGroup.MarginLayoutParams(holder.textview.getLayoutParams()); marginLayoutParams_text.setMargins(text_left_margin,19,0,0); RelativeLayout.LayoutParams layoutParams_text = new RelativeLayout.LayoutParams(marginLayoutParams_text); holder.textview.setLayoutParams(layoutParams_text); holder.textview.setText( sub_string(info.get_item_name())); } } @Override public int getItemCount() { return listitem.size(); } String sub_string(String source_string) { String sub_string = ""; if(source_string.length()<8) { sub_string = source_string; } else { sub_string=source_string.substring(0, 4) "..." source_string.substring(source_string.length() - 4); } return sub_string; } }
SpacesItemDecoration.java
package com.example.navigation; import android.graphics.Rect; import android.view.View; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int left; private int right; private int top; private int bottom; public SpacesItemDecoration(Rect rect) { this.left = rect.left; this.right = rect.right; this.top = rect.top; this.bottom = rect.bottom; } @Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { outRect.left = this.left; outRect.bottom = this.bottom; //由于每行都只有5个,所以第一个都是3的倍数,把左边距设为0 if (parent.getChildLayoutPosition(view) %4==0) { outRect.left = 0; } } }
xml
<androidx.recyclerview.widget.RecyclerView android:id="@ id/recycler_view" android:layout_marginTop="174px" android:layout_alignParentRight="true" android:layout_width="1660px" android:layout_marginRight="100px" android:layout_height="match_parent" />
希望我可以按照计划完成任务呀!!!!!