|
| 1 | +package com.mcxiaoke.next.samples; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.os.SystemClock; |
| 6 | +import android.support.annotation.NonNull; |
| 7 | +import android.support.annotation.Nullable; |
| 8 | +import android.support.v7.widget.RecyclerView.ViewHolder; |
| 9 | +import android.view.LayoutInflater; |
| 10 | +import android.view.View; |
| 11 | +import android.view.ViewGroup; |
| 12 | +import android.widget.TextView; |
| 13 | +import com.mcxiaoke.next.recycler.AdvancedRecyclerArrayAdapter; |
| 14 | +import com.mcxiaoke.next.recycler.AdvancedRecyclerView; |
| 15 | +import com.mcxiaoke.next.recycler.AdvancedRecyclerView.ItemViewHolder; |
| 16 | +import com.mcxiaoke.next.recycler.AdvancedRecyclerView.OnLoadDataListener; |
| 17 | +import com.mcxiaoke.next.recycler.AdvancedRecyclerView.ViewHolderCreator; |
| 18 | +import com.mcxiaoke.next.task.SimpleTaskCallback; |
| 19 | +import com.mcxiaoke.next.task.TaskBuilder; |
| 20 | +import com.mcxiaoke.next.utils.LogUtils; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Random; |
| 25 | +import java.util.concurrent.Callable; |
| 26 | + |
| 27 | +/** |
| 28 | + * User: mcxiaoke |
| 29 | + * Date: 2018/6/17 |
| 30 | + * Time: 17:53 |
| 31 | + */ |
| 32 | +public class RecyclerViewSample extends BaseActivity { |
| 33 | + public static final String TAG = "NextRecyclerViewSample"; |
| 34 | + |
| 35 | + private AdvancedRecyclerView recyclerView; |
| 36 | + private SampleAdapter arrayAdapter; |
| 37 | + private Random random = new Random(); |
| 38 | + private static int sCounter = 0; |
| 39 | + |
| 40 | + static class SimpleViewHolder extends ItemViewHolder { |
| 41 | + private TextView textView; |
| 42 | + |
| 43 | + public SimpleViewHolder(final View itemView) { |
| 44 | + super(itemView); |
| 45 | + textView = itemView.findViewById(R.id.text1); |
| 46 | + } |
| 47 | + |
| 48 | + @SuppressLint("SetTextI18n") |
| 49 | + @Override |
| 50 | + public void bind(final int position) { |
| 51 | + super.bind(position); |
| 52 | + textView.setText("Header Text " + position); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void onCreate(final Bundle savedInstanceState) { |
| 58 | + super.onCreate(savedInstanceState); |
| 59 | + setContentView(R.layout.act_next_recycler_view); |
| 60 | + |
| 61 | + arrayAdapter = new SampleAdapter(); |
| 62 | + recyclerView = findViewById(R.id.recycler_view); |
| 63 | + recyclerView.setAdapter(arrayAdapter); |
| 64 | + recyclerView.setEnableHeaderLoading(true); |
| 65 | + recyclerView.setEnableFooterLoading(true); |
| 66 | + recyclerView.setOnLoadDataListener(new OnLoadDataListener() { |
| 67 | + @Override |
| 68 | + public void onHeaderLoading(final AdvancedRecyclerView recyclerView) { |
| 69 | + addHeaderDataAsync(); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onFooterLoading(final AdvancedRecyclerView recyclerView) { |
| 75 | + addFooterDataAsync(); |
| 76 | + } |
| 77 | + }); |
| 78 | + recyclerView.addHeader(R.layout.layout_simple_header); |
| 79 | + arrayAdapter.addAll(Data.TITLES); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + private void addHeaderDataAsync() { |
| 84 | + TaskBuilder.create(new Callable<List<String>>() { |
| 85 | + @Override |
| 86 | + public List<String> call() throws Exception { |
| 87 | + SystemClock.sleep(random.nextInt(8) * 1000); |
| 88 | + return generateData(random.nextInt(30)); |
| 89 | + } |
| 90 | + }).callback(new SimpleTaskCallback<List<String>>() { |
| 91 | + |
| 92 | + @Override |
| 93 | + public void onTaskSuccess(final List<String> strings, final Bundle extras) { |
| 94 | + super.onTaskSuccess(strings, extras); |
| 95 | + LogUtils.i(TAG, "addHeaderDataAsync thread=" + Thread.currentThread()); |
| 96 | + arrayAdapter.addAll(0, strings); |
| 97 | + recyclerView.setHeaderLoading(false); |
| 98 | + recyclerView.setEnableHeaderLoading(false); |
| 99 | + recyclerView.addHeader(new ViewHolderCreator<ViewHolder>() { |
| 100 | + @Override |
| 101 | + public ViewHolder create(final ViewGroup parent) { |
| 102 | + LayoutInflater inflater = LayoutInflater.from(parent.getContext()); |
| 103 | + View view = inflater.inflate(R.layout.layout_simple_header, parent, false); |
| 104 | + return new SimpleViewHolder(view); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void bind(final ViewHolder holder, final int position) { |
| 109 | + |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void onTaskFailure(final Throwable ex, final Bundle extras) { |
| 116 | + super.onTaskFailure(ex, extras); |
| 117 | + } |
| 118 | + }).with(this).start(); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + private void addFooterDataAsync() { |
| 123 | + TaskBuilder.create(new Callable<List<String>>() { |
| 124 | + @Override |
| 125 | + public List<String> call() throws Exception { |
| 126 | + SystemClock.sleep(random.nextInt(8) * 1000); |
| 127 | + return generateData(random.nextInt(30)); |
| 128 | + } |
| 129 | + }).callback(new SimpleTaskCallback<List<String>>() { |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onTaskSuccess(final List<String> strings, final Bundle extras) { |
| 133 | + super.onTaskSuccess(strings, extras); |
| 134 | + LogUtils.i(TAG, "addFooterDataAsync thread=" + Thread.currentThread()); |
| 135 | + arrayAdapter.addAll(strings); |
| 136 | + recyclerView.setFooterLoading(false); |
| 137 | + if (sCounter > 10) { |
| 138 | + recyclerView.addFooter(R.layout.layout_simple_header); |
| 139 | + recyclerView.setEnableFooterLoading(false); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void onTaskFailure(final Throwable ex, final Bundle extras) { |
| 145 | + super.onTaskFailure(ex, extras); |
| 146 | + } |
| 147 | + }).with(this).start(); |
| 148 | + } |
| 149 | + |
| 150 | + private List<String> generateData(int count) { |
| 151 | + sCounter++; |
| 152 | + final List<String> data = new ArrayList<>(); |
| 153 | + for (int i = 0; i < 10 + count; i++) { |
| 154 | + data.add("List Item AAA " + sCounter + " - No. " + i); |
| 155 | + } |
| 156 | + return data; |
| 157 | + } |
| 158 | + |
| 159 | + static class SampleAdapter extends AdvancedRecyclerArrayAdapter<String, ItemViewHolder> { |
| 160 | + |
| 161 | + @NonNull |
| 162 | + @Override |
| 163 | + public ItemViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) { |
| 164 | + View view = LayoutInflater.from(parent.getContext()) |
| 165 | + .inflate(android.R.layout.simple_list_item_1, parent, false); |
| 166 | + return new ItemViewHolder(view); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void onBindViewHolder(@NonNull final ItemViewHolder holder, final int position) { |
| 171 | + TextView textView = holder.itemView.findViewById(android.R.id.text1); |
| 172 | + textView.setText(getItem(position)); |
| 173 | + } |
| 174 | + |
| 175 | + @Nullable |
| 176 | + @Override |
| 177 | + public Object getItemId(@NonNull final String item) { |
| 178 | + return item; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments