从GitHub上下载的xRecyclerView的demo导入studio后出现这个异常
xRecyclerView源码地址:https://github.com/jianghejie/XRecyclerView
另外感谢这位博主的开源
这是导入GitHub上的demo出现的异常解决办法部分
解决办法:
看看截图就解决问题是不是很简单,当然如果以后再GitHub上下载其他demo遇到这个错误时也可以通过这个办法解决
1:xRecyclerView的功能和RecyclerView大体差不多,但是他能解决多个滑动组合到一个页面会出现加载缓慢,卡顿等。
特别好的是比如下面这个截图只需要设置头视图即可不需要写另外的布局文件
代码如下:
package com.lt.liwushuov10.liwuwyp;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lt.liwushuov10.R;
import com.lt.liwushuov10.helper.OkHttpClientHelper;
import com.lt.liwushuov10.helper.SpacesItemDecoration;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//优化:下拉刷新进度条样式的优化
/**
* A simple {@link Fragment} subclass.
*/
public class DummyFragment extends Fragment {
private Context mContext = null;
private XRecyclerView xRecyclerview_dummy_fragment;
private ImageView imageview_dummyfragment_xrecycler;
private List<BangdanBean.DataBean.ItemsBean> mListBean = new ArrayList<>();
private int sentTime = 0;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
private String url;
private XRecyclerViewDummyAdapter xRecyclerViewDummyAdapter;
//单例模式--动态生成小fragment
public static DummyFragment newInstance(String url, String titleTop) {
Bundle args = new Bundle();
args.putString("url", url);
DummyFragment fragment = new DummyFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
Bundle bundle = getArguments();
url = bundle.getString("url");//获得网址
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_dummy, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//初始化数据
initData();
initView(view);
}
//用于加载网络数据
private void initData() {
new Thread(new Runnable() {
private String cover_image;
@Override
public void run() {
//okhttp加载的网络数据
try {
String stringFromURL = OkHttpClientHelper.getStringFromURL(mContext, url, null);
BangdanBean bangdanBean = parseJsonToBean(stringFromURL);
mListBean = bangdanBean.getData().getItems();
cover_image = bangdanBean.getData().getCover_image();//头部视图的图片
handler.sendEmptyMessageAtTime(1, 500);
//利用handler.post返回ui线程
handler.post(new Runnable() {
@Override
public void run() {
Glide.with(mContext).load(cover_image).into(imageview_dummyfragment_xrecycler);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();//记得开启线程
}
private void initView(View view) {
xRecyclerview_dummy_fragment = (XRecyclerView) view.findViewById(R.id.xRecyclerview_dummy_fragment);
//必选设置--网格布局
GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
xRecyclerview_dummy_fragment.setLayoutManager(gridLayoutManager);
xRecyclerview_dummy_fragment.setHasFixedSize(true);
xRecyclerview_dummy_fragment.setItemAnimator(new DefaultItemAnimator());
xRecyclerview_dummy_fragment.addItemDecoration(new SpacesItemDecoration(10));
//下拉刷新的進度條樣式
xRecyclerview_dummy_fragment.setRefreshProgressStyle(ProgressStyle.BallBeat);
//上拉加載的進度條樣式
xRecyclerview_dummy_fragment.setLaodingMoreProgressStyle(ProgressStyle.Pacman);
//添加头视图
View header = LayoutInflater.from(mContext).inflate(R.layout.xrecycler_image_header, null);//引入头部imageview布局
//找到imageview的id,为了从网络上下载图片数据
imageview_dummyfragment_xrecycler = (ImageView) header.findViewById(R.id.imageview_dummyfragment_xrecycler);
xRecyclerview_dummy_fragment.addHeaderView(header);
xRecyclerViewDummyAdapter = new XRecyclerViewDummyAdapter(mContext, mListBean);
/* 适配器中接口回调实现的item点击事件*/
xRecyclerViewDummyAdapter.OnItemClickListener(new XRecyclerViewDummyAdapter.OnItemClickListener() {
@Override//单击跳转到webview的activity页面
public void onItemClick(View view, int position) {
String url = mListBean.get(position-2).getUrl();
Intent intent=new Intent(mContext,BangdanWebActivity.class);
intent.putExtra("url",url);
startActivity(intent);
}
@Override
public void onLongItemClick(View view, int position) {
}
});
//下载的监听
xRecyclerview_dummy_fragment.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override//下拉刷新
public void onRefresh() {
sentTime++;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mListBean.clear();
initData();//加载数据
xRecyclerViewDummyAdapter.notifyDataSetChanged();
xRecyclerview_dummy_fragment.refreshComplete();
}
}, 2000);
}
@Override//上拉加载--此项目没有上拉加载
public void onLoadMore() {
xRecyclerview_dummy_fragment.loadMoreComplete();
}
});
// //XrecyclerView的item点击事件
// xRecyclerview_dummy_fragment.addOnItemTouchListener();
//设置适配器
xRecyclerview_dummy_fragment.setAdapter(xRecyclerViewDummyAdapter);
}
//解析网络数据的方法
public BangdanBean parseJsonToBean(String url) {
Gson gson = new Gson();
return gson.fromJson(url, new TypeToken<BangdanBean>() {
}.getType());
}
}