android recyclerView嵌套progressbar

Posted wxmwanggood

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
Log.d(Tag,first_position + " "+ last_position);
}
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i < 101; i++) {
final int progress = i;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
publishProgress(positionInAdapter, progress);
}
});
SystemClock.sleep(5000);
}
}
}).start();
}

public void publishProgress(final int positionInAdapter, final int progress_time) {
list.get(positionInAdapter).set_item_progress(progress_time);
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager)
{
GridLayoutManager gridmanager = (GridLayoutManager) layoutManager;
if(positionInAdapter >= gridmanager.findFirstVisibleItemPosition() &&
positionInAdapter <= gridmanager.findLastVisibleItemPosition())
{
int positionInListView = positionInAdapter - gridmanager.findFirstVisibleItemPosition();
ProgressBar item = (ProgressBar) recyclerView.getChildAt(positionInListView)
.findViewById(R.id.pb_listening_progressbar);
item.setProgress(progress_time);
}
}
}
}
 
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" />

 

 希望我可以按照计划完成任务呀!!!!!

以上是关于android recyclerView嵌套progressbar的主要内容,如果未能解决你的问题,请参考以下文章

android中RecyclerView嵌套RecyclerView中,如何为内层RecyclerView设值的问题

Android番外篇 NestedScrollView嵌套RecyclerView

android recyclerView嵌套progressbar

android中RecyclerView嵌套问题中,内层RecyclerView区域无法响应Item点击事件

Android Scrollview嵌套RecyclerView导致滑动卡顿问题解决

Android之SwipeRefreshLayout嵌套RecyclerView遇到的坑