Scrollview+listview的不足
最近在做项目的时候,发现在使用scrollview+listview时候,总会遇到控件显示不全,这让我感到非常的头疼,看到网上方法尝试了下,发现对于动态扩展的内容还是会出现控件内容显示不全的问题,所以,我找到了一种替代Scrollview+listview的不足 方法,使用NestedScrollView+RecyclerView的方法替代
NestedScrollView+RecyclerView的界面布局
友情提示:scrollview只能包含在整个的布局中
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/mylv"
android:divider="@null"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.NestedScrollView>
适配器文件
public class RYPingLunAdapter extends RecyclerView.Adapter<RYPingLunAdapter.ViewHolder> {
Context context;
List<PingLunBean> pllist;
public RYPingLunAdapter(Context context, List<PingLunBean> pllist) {
this.context = context;
this.pllist = pllist;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.itempinglunbeanlayout, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.nametv.setText(pllist.get(position).getName()+" "+pllist.get(position).getTime());
// holder.content.setText(pllist.get(position).getContent());
MyMoreTextView myMoreTextView = new MyMoreTextView();
myMoreTextView .getLastIndexForLimit(holder.content, 5, pllist.get(position).getContent());
}
@Override
public int getItemCount() {
return pllist.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
TextView nametv;
TextView content;
public ViewHolder (View view)
{
super(view);
nametv = view.findViewById(R.id.nametv);
content = view.findViewById(R.id.content);
}
}
}
代码调用
mylv = findViewById(R.id.mylv);
mylv.setNestedScrollingEnabled(false);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mylv.setLayoutManager(layoutManager);
RYPingLunAdapter pingLunAdapter = new RYPingLunAdapter(jianyanzizhengdetailactivity.this,pllist);
mylv.setAdapter(pingLunAdapter);
pingLunAdapter.notifyDataSetChanged();
网友评论