Adapter
package adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bwei.week03exe22190112.R;
import java.util.List;
import bean.NewsBean;
public class StickyExampleAdapter extends RecyclerView.Adapter {
//第一个吸顶
private static final int FIRST_STICKY_VIEW = 1;
//别的吸顶
public static final int HAS_STICKY_VIEW = 2;
//正常View
public static final int NONE_STICKY_VIEW = 3;
private final LayoutInflater mInflate;
private final List datas;
public StickyExampleAdapter(Context context, List datas) {
mInflate = LayoutInflater.from(context);
this.datas = datas;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = mInflate.inflate(R.layout.item_ui, parent, false);
return new RecyclerViewHolder(inflate);
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
NewsBean stickyBean = datas.get(position);
holder.tvName.setText(stickyBean.name);
holder.tvGender.setText(stickyBean.autor);
if (position == 0) {
holder.tvStickyHeader.setVisibility(View.VISIBLE);
holder.tvStickyHeader.setText(stickyBean.sticky);
holder.itemView.setTag(FIRST_STICKY_VIEW);
} else {
if (!TextUtils.equals(stickyBean.sticky, datas.get(position - 1).sticky)) {
holder.tvStickyHeader.setVisibility(View.VISIBLE);
holder.tvStickyHeader.setText(stickyBean.sticky);
holder.itemView.setTag(HAS_STICKY_VIEW);
} else {
holder.tvStickyHeader.setVisibility(View.GONE);
holder.itemView.setTag(NONE_STICKY_VIEW);
}
}
//通过此处设置ContentDescription,作为内容描述,可以通过getContentDescription取出,功效跟setTag差不多。
holder.itemView.setContentDescription(stickyBean.sticky);
}
@Override
public int getItemCount() {
return datas == null ? 0 : datas.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView tvStickyHeader;
RelativeLayout rlContentWrapper;
TextView tvName;
TextView tvGender;
RecyclerViewHolder(View itemView) {
super(itemView);
tvStickyHeader = (TextView) itemView.findViewById(R.id.tv_sticky_header_view);
rlContentWrapper = (RelativeLayout) itemView.findViewById(R.id.rl_content_wrapper);
tvName = (TextView) itemView.findViewById(R.id.name);
tvGender = (TextView) itemView.findViewById(R.id.auto);
}
}
}
MainActivity
package com.bwei.week03exe22190112;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import adapter.StickyExampleAdapter;
import bean.News;
import bean.NewsBean;
import contactss.Contacts;
import presenter.MainPresenter;
import view.IView;
public class MainActivity extends AppCompatActivity implements IView {
private TextView tvStickyHeaderView;
private RecyclerView recyclerView;
private MainPresenter presenter = new MainPresenter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Map map = new HashMap<>();
map.put("uid", "71");
presenter.getData(Contacts.data_url, map, News.class);
initView();
initListener();
}
/**
* 初始化View
*/
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recycle);
tvStickyHeaderView = (TextView) findViewById(R.id.tv_sticky_header_view);
}
/**
* 初始化Listener
*/
private void initListener() {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
View stickview = recyclerView.findChildViewUnder(0, 0);
if (stickview != null && stickview.getContentDescription() != null) {
if (!TextUtils.equals(tvStickyHeaderView.getText(), stickview.getContentDescription())) {
tvStickyHeaderView.setText(stickview.getContentDescription());
}
}
View transInfoView = recyclerView.findChildViewUnder(
0, tvStickyHeaderView.getHeight() + 1);
if (transInfoView.getTag() != null) {
int transViewStatus = (int) transInfoView.getTag();
int top = transInfoView.getTop();
if (transViewStatus == StickyExampleAdapter.HAS_STICKY_VIEW) {
if (top > 0) {
int dealtY = top - tvStickyHeaderView.getMeasuredHeight();
tvStickyHeaderView.setTranslationY(dealtY);
} else {
tvStickyHeaderView.setTranslationY(0);
}
} else if (transViewStatus == StickyExampleAdapter.NONE_STICKY_VIEW) {
tvStickyHeaderView.setTranslationY(0);
}
}
}
});
}
public void getData(List newsData) {
List stickyExampleModels = new ArrayList<>();
for (int i = 0; i < newsData.size(); i++) {
for (int j = 0; j newsData = news.getData();
Log.d("dataBean", "onSuccess: " + newsData.get(0).getSpus().get(0).toString());
getData(newsData);
}
@Override
public void onFail(String fail) {
}
}
主布局
子条目
Bean
public class StickyBean {
public String name;
public String autor;
public String sticky;
public StickyBean(String sticky, String name, String autor) {
this.sticky = sticky;
this.name = name;
this.autor = autor;
}
}
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:recyclerview-v7:28.0.0'