android recyclerView嵌套progressbar
刚刚实现了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.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class FragmentTwo extends Fragment {
private View mRootView;
private RecyclerView recyclerView;
private PlayListAdapter adapter;
private List<Playlist_Info> list = new ArrayList<>();
private String Tag = "FragmentTwo";
@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);
progress_bar(1);
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 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));
}
}
}
private void progress_bar(final int positionInAdapter) {
list.get(positionInAdapter).setIsPlaying(true);
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager)
{
GridLayoutManager gridmanager = (GridLayoutManager) layoutManager;
int first_position = gridmanager.findFirstVisibleItemPosition();
int last_position = gridmanager.findLastVisibleItemPosition();
if(positionInAdapter >= gridmanager.findFirstVisibleItemPosition() &&
positionInAdapter <= gridmanager.findLastVisibleItemPosition())
{
int positionInListView = positionInAdapter - gridmanager.findFirstVisibleItemPosition();
ProgressBar item_listening = (ProgressBar) recyclerView.getChildAt(positionInListView)
.findViewById(R.id.pb_listening_progressbar);
item_listening.setVisibility(View.VISIBLE);