有人可以编辑此代码吗? 这不会显示任何带有recyclerview的项目 哪里错了?可以给我编辑的例子吗? 或指示我错在哪里。
MainActivity.java
package com.numetriclabz.recyclerviewwithsearch;
import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
public SearchView search;
Adapter mAdapter;
protected Handler handler;
private List<Actors> list = new ArrayList<Actors>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
List<Actors> data = createlist(); // in this method, Create a list of items.
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new Adapter(data, getApplication(), mRecyclerView );
mRecyclerView.setAdapter(mAdapter);
handler = new Handler();
mAdapter.setOnLoadMoreListener(new Adapter.OnLoadMoreListener() {
@Override
public void onLoadMore() {
//add progress item
list.add(null);
mAdapter.notifyItemInserted(list.size() - 1);
handler.postDelayed(new Runnable() {
@Override
public void run() {
//remove progress item
list.remove(list.size() - 1);
mAdapter.notifyItemRemoved(list.size());
//add items one by one
for (int i = 0; i < 15; i++) {
list.add(new Actors("b" + (list.size() + 1)));
mAdapter.notifyItemInserted(list.size());
}
mAdapter.setLoaded();
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
}
}, 2000);
System.out.println("load");
}
});
}
// this method is used to create list of items.
public List<Actors> createlist(){
List<Actors> data = new ArrayList<>();
for(int i=1; i<=12;i++) {
list.add(new Actors("a"+i));
}
return data;
}
}
Adapter.java
package com.numetriclabz.recyclerviewwithsearch;
import android.content.Context;
import android.provider.ContactsContract;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Collections;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<View_Holder> {
private List<Actors> mDataset = Collections.emptyList();
// The minimum amount of items to have below your current scroll position before loading more.
private int visibleThreshold = 2;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
protected Context context;
public Adapter(List<Actors> myDataSet, Context context, RecyclerView recyclerView) {
this.mDataset = myDataSet;
this.context = context;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
});
}
}
@Override
public View_Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
View_Holder holder = new View_Holder(v);
return holder;
}
@Override
public void onBindViewHolder(View_Holder holder, int position) {
holder.title.setText(mDataset.get(position).name);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
// Insert a new item to the RecyclerView
public void insert(int position, Actors data) {
mDataset.add(position, data);
notifyItemInserted(position);
}
// Remove a RecyclerView item containing the Data object
public void remove(Actors data) {
int position = mDataset.indexOf(data);
mDataset.remove(position);
notifyItemRemoved(position);
}
public void setLoaded() {
loading = false;
}
@Override
public int getItemCount() {
return mDataset.size();
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
public interface OnLoadMoreListener {
void onLoadMore();
}
public static class TextViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public TextViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(android.R.id.text1);
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
}
}
}
Actors.java
package com.numetriclabz.recyclerviewwithsearch;
public class Actors {
public String name;
Actors(String name) {
this.name = name;
}
}
View_Holder.java
package com.numetriclabz.recyclerviewwithsearch;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
public class View_Holder extends RecyclerView.ViewHolder {
CardView cv;
TextView title;
View_Holder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
title = (TextView) itemView.findViewById(R.id.title);
}
}
OnLoadMoreListener.java
package com.numetriclabz.recyclerviewwithsearch;
public interface OnLoadMoreListener {
void onLoadMore();
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.numetriclabz.recyclerviewwithsearch.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_marginTop="8dp"/>
</RelativeLayout>
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
progressbar_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content" />
</LinearLayout>
答案 0 :(得分:1)
你在这里做错了
public List<Actors> createlist(){
List<Actors> data = new ArrayList<>();
for(int i=1; i<=12;i++) {
list.add(new Actors("a"+i));
}
return data;
}
此处list.add(new Actors("a"+i));
您要在list
对象中添加数据,但在return语句中,return data;
表示您的data
列表为空。所以改变它
for(int i=1; i<=12;i++) {
data.add(new Actors("a"+i));
}
或将return data;
更改为return list;
答案 1 :(得分:0)
在MainActivity
中使用新的LinearLayoutManager(this)进行简单替换new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);