fragment 滑动加载
package com.example.myapplication;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import androidx.annotation.
·
package com.example.myapplication;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.fastjson.JSONObject;
import com.example.myapplication.adapter.CellListAdapter;
import com.example.myapplication.entity.Article;
import com.example.myapplication.entity.FirstPageVO;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
static ArrayList<Article> data = new ArrayList<>();
// ListView listView;
static RecyclerView recyclerView;
private static int total = 100;
private static final int size = 5;
private static int pages = 10;
private static int page = 1;
CellListAdapter adapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, container, false);
// ViewGroup pa = (ViewGroup) rootView.getParent();
// if(pa!=null) {
// pa.removeView(rootView);
// }
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView = requireActivity().findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
adapter = new CellListAdapter(requireActivity(), data);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
protected boolean isSlideToBottom(RecyclerView recyclerView) {
if (recyclerView == null) return false;
if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange()) {
return true;
}
return false;
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(isSlideToBottom(recyclerView)) {
getFirstPageData();
}
}
});
// Article article = new Article();
// article.setTitle("6666");
// data.add(article);
// adapter = new CellListAdapter(getActivity(), data);
// listView.setAdapter(adapter);
getFirstPageData();
}
public void getFirstPageData() {
//如果超过页数,就不继续请求数据了
if (data.size() >= total) {
return;
}
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.itshare.club/api/blogList?page=" + page + "&size=" + size)
.get().build();
Call call = client.newCall(request);
page++;
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
// System.out.println(("res"+ response.body().string() ));
FirstPageVO json = JSONObject.parseObject(response.body().string(), FirstPageVO.class);
FirstPageVO.Data d = json.getData();
total = d.getTotal();
pages = d.getPages();
data.addAll(d.getContentList());
getActivity().runOnUiThread(() -> {
Log.i("request .", "success" + page);
// adapter.setList(data);
adapter.notifyDataSetChanged();
});
;
}
});
}
// @Override
// public void switchContent(Fragment from, Fragment to) {
// if (mContent != to) {
// mContent = to;
// FragmentTransaction transaction = mFragmentMan.beginTransaction().setCustomAnimations(
// android.R.anim.fade_in, R.anim.slide_from_right);
// if (!to.isAdded()) { // 先判断是否被add过
// transaction.hide(from).add(R.id.content_frame, to).commit(); // 隐藏当前的fragment,add下一个到Activity中
// } else {
// transaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
// }
// }
// }
// public boolean isFragmentViewInit = false;
// private View lastView;
// @Override
// public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
// if (!isFragmentViewInit) {
// super.onViewCreated(view, savedInstanceState);
//
// isFragmentViewInit = true;
// }
//
// }
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Bundle savedInstanceState) {
// // Log.i(TAG, "onCreateView");
//
// if (rootView == null) {
// rootView = inflater.inflate(R.layout.home_fragment, null);
// }
// // 缓存的rootView需要判断是否已经被加过parent,如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。
// ViewGroup parent = (ViewGroup) rootView.getParent();
// if (parent != null) {
// parent.removeView(rootView);
// }
// return rootView;
// }
}
package com.example.myapplication.adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.entity.Article;
import java.util.List;
/**
* @Author lyr
* @create 2020/10/9 12:44
*/
public class CellListAdapter /*extends BaseAdapter */ extends RecyclerView.Adapter<CellListAdapter.ViewHolder>{
List<Article> list;
LayoutInflater inflater;
public CellListAdapter(Context context, List<Article> list) {
this.list = list;
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.cell_item, parent,false);
ViewHolder viewHolder = new ViewHolder(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("hello","world");
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Article article = list.get(position);
holder.title.setText(article.getTitle());
holder.description.setText(article.getDescription());
Glide.with(holder.imageView.getContext())
.load(article.getFirstPicture())
.into(holder.imageView);
}
@Override
public int getItemCount() {
return list.size();
}
// ViewHolder viewHolder;
class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView imageView;
TextView description;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.a_title);
imageView = itemView.findViewById(R.id.a_imageView);
description = itemView.findViewById(R.id.a_description);
}
}
//
// // @Override
// public View getView(int i, View convertView, ViewGroup viewGroup) {
// ViewHolder viewHolder;
// // 当view为空时才加载布局,否则,直接修改内容
// if (convertView == null) {
//
// // 通过inflate的方法加载布局,context需要在使用这个Adapter的Activity中传入。
// convertView = inflater.inflate(R.layout.cell_item, null);
// viewHolder = new ViewHolder();
// viewHolder.title = convertView.findViewById(R.id.a_title);
// viewHolder.imageView = convertView.findViewById(R.id.a_imageView);
// viewHolder.description = convertView.findViewById(R.id.a_description);
// //缓存 viewHolder 对象
// convertView.setTag(viewHolder);
//
// // 用setTag方法将处理好的viewHolder放入view中
// }else {
// viewHolder = (ViewHolder)convertView.getTag();
// }
// Article article = this.list.get(i);
//
// // imageView.setImageURI(Uri.parse("http://img.itshare.club/20200915222524.png"));
// Glide.with(viewHolder.imageView.getContext())
// .load(article.getFirstPicture())
// .into(viewHolder.imageView);
// viewHolder.title.setText(article.getTitle());
// viewHolder.description.setText(article.getDescription());
//
// // 从viewHolder中取出对应的对象,然后赋值给他们
// // viewHolder.title.setText(list.get(i).getTitle());
//
// // 将这个处理好的view返回
// return convertView;
// }
}
监听点击事件
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.cell_item, parent,false);
ViewHolder viewHolder = new ViewHolder(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("hello","world");
}
});
return viewHolder;
}
更多推荐
已为社区贡献3条内容
所有评论(0)