风流不在谈锋胜,袖手无言味最长。这篇文章主要讲述如何在android中单击listview适配器时展开和折叠视图相关的知识,希望能为你提供帮助。
我正在开发android应用程序,其中我想向用户显示一些优惠券,为此我使用自定义ListView
并设置自定义布局。在自定义布局我想扩展和折叠相关布局,当点击textview
,下面是我的代码为这个:
【如何在android中单击listview适配器时展开和折叠视图】Coupon_Layout.xml:
<
?xml version="1.0" encoding="utf-8"?>
<
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<
TextView
android:id="@+id/coupon_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:background="@drawable/dash_rectangular_background"
android:padding="15dp"
android:text="FIRST10"
android:textColor="@color/input_login"
android:textSize="16.5sp" />
<
TextView
android:id="@+id/coupon_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/coupon_code"
android:layout_alignBottom="@+id/coupon_code"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/coupon_code"
android:maxLines="2"
android:text="Get 10?/- off on your 1st ride*."
android:textColor="@color/input_login"
android:textSize="16sp" />
<
TextView
android:id="@+id/coupon_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/coupon_code"
android:layout_below="@+id/coupon_percentage"
android:layout_marginTop="24dp"
android:ellipsize="end"
android:maxLines="2"
android:text="*Only for new user."
android:textColor="@color/input_login"
android:textSize="16sp" />
<
TextView
android:id="@+id/tv_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/coupon_details"
android:layout_marginStart="5dp"
android:layout_toEndOf="@+id/coupon_details"
android:background="@null"
android:text="Details"
android:textSize="16sp"
android:textColor="@color/bg_main"
android:textAllCaps="false"/>
<
RelativeLayout
android:id="@+id/details_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/coupon_details"
android:layout_marginTop="10dp">
<
ImageView
android:id="@+id/imageView2"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:background="#0000"
android:src="https://www.songbingjia.com/android/@drawable/ic_dot_bullet" />
<
TextView
android:id="@+id/tv_limitation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="19dp"
android:layout_toEndOf="@+id/imageView2"
android:text="You can avail this offer on your 1st ride"
android:textSize="15sp" />
<
/RelativeLayout>
<
View
android:id="@+id/v_seprator_coupon_2"
style="@style/Divider_History"
android:layout_below="@+id/details_layout"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp" />
<
/RelativeLayout>
coupon_adapter.java
public class Coupon_Adapter extends BaseAdapter{private Activity activity;
private LayoutInflater inflater;
private List<
Coupon_List>
couponList;
private TextView coupon_description,coupon_percentage,coupon_code,details,limitations;
private Coupon_List coupon_list;
private RelativeLayout details_layout;
private boolean isexpand = false;
public Coupon_Adapter(Activity activity, List<
Coupon_List>
couponList) {
this.activity = activity;
this.couponList = couponList;
}@Override
public int getCount() {
return couponList.size();
}@Override
public Object getItem(int i) {
return couponList.get(i);
}@Override
public long getItemId(int i) {
return i;
}@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(inflater == null){
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(view == null){
view = inflater.inflate(R.layout.coupon_layout,null);
}
coupon_code = view.findViewById(R.id.coupon_code);
coupon_description =view.findViewById(R.id.coupon_details);
coupon_percentage = view.findViewById(R.id.coupon_percentage);
limitations = view.findViewById(R.id.tv_limitation);
details = view.findViewById(R.id.tv_details);
details_layout = view.findViewById(R.id.details_layout);
collapse(details_layout);
details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!isexpand) {
Log.d("texview onclick listner","true");
expand(details_layout);
}else {
collapse(details_layout);
}
}
});
coupon_list = couponList.get(i);
coupon_code.setText(coupon_list.getCoupon_code());
Log.d("coupon list",coupon_list.getCoupon_code());
coupon_percentage.setText(coupon_list.getCoupon_percentage());
// Log.d("coupon list",coupon_list.getCoupon_description());
coupon_description.setText(coupon_list.getCoupon_detail_info());
Log.d("coupon list",coupon_list.getCoupon_limitation());
limitations.setText(coupon_list.getCoupon_detail_info());
return view;
}public void expand(final View v) {
v.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? RelativeLayout.LayoutParams.WRAP_CONTENT
: (int)(targetHeight * interpolatedTime);
v.requestLayout();
}@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
isexpand = true;
}public void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation()
{
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
isexpand = false;
}
}
我使用该方法来扩展和折叠视图,它在其他活动中工作,我调试代码,当我点击
textview
它进入setOnClickListener
但不扩展或折叠我的视图。请帮助我解决这个问题。答案改变太多了。将您的drawable和颜色添加到文本中,因为我在测试时删除了它们,这里我修改了您的代码和xml。
<
?xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:animationCache="true"
android:layout_height="match_parent">
<
RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<
TextView
android:id="@+id/coupon_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:padding="15dp"
android:text="FIRST10"
android:textSize="16.5sp" />
<
TextView
android:id="@+id/coupon_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/coupon_code"
android:layout_alignBottom="@+id/coupon_code"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/coupon_code"
android:maxLines="2"
android:text="Get 10?/- off on your 1st ride*."android:textSize="16sp" />
<
TextView
android:id="@+id/coupon_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/coupon_code"
android:layout_below="@+id/coupon_percentage"
android:layout_marginTop="24dp"
android:ellipsize="end"
android:maxLines="2"
android:text="*Only for new user."
android:textSize="16sp" />
<
TextView
android:id="@+id/tv_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/coupon_details"
android:layout_marginStart="5dp"
android:layout_toEndOf="@+id/coupon_details"
android:background="@null"
android:text="Details"
android:textAllCaps="false"
android:textSize="16sp" />
<
RelativeLayout
android:id="@+id/details_layout"
android:layout_width="wrap_content"
android:background="@color/colorAccent"
android:layout_height="wrap_content"android:layout_below="@+id/coupon_details"
android:layout_marginTop="10dp">
<
ImageView
android:id="@+id/imageView2"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:background="#0000" />
<
TextView
android:id="@+id/tv_limitation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="19dp"
android:layout_toEndOf="@+id/imageView2"
android:text="You can avail this offer on your 1st ride"
android:textSize="15sp" />
<
/RelativeLayout>
<
/RelativeLayout>
<
/LinearLayout>
并在适配器类中进行更改,即Coupon_Adapter
//在适配器类的构造函数中添加此行。
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
更改你的onView并添加你的代码,我还提供了一种更简单的方法来显示隐藏视图。
@Override
public View getView(int i, View view, ViewGroup viewGroup) {final Holder listViewHolder;
if (view == null) {
listViewHolder = new Holder();
view = inflater.inflate(R.layout.coupon_layout,viewGroup,false);
listViewHolder.coupon_code = view.findViewById(R.id.coupon_code);
listViewHolder.coupon_description =view.findViewById(R.id.coupon_details);
listViewHolder.coupon_percentage = view.findViewById(R.id.coupon_percentage);
listViewHolder.limitations = view.findViewById(R.id.tv_limitation);
listViewHolder.details = view.findViewById(R.id.tv_details);
listViewHolder.details_layout = view.findViewById(R.id.details_layout);
view.setTag(listViewHolder);
} else {
listViewHolder = (Holder) view.getTag();
}collapse(listViewHolder.details_layout);
listViewHolder.coupon_code.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!isexpand) {
Log.d("texview onclick listner","true");
expand(listViewHolder.details_layout);
}else {
collapse(listViewHolder.details_layout);
}/*if(listViewHolder.details_layout.getVisibility() == View.VISIBLE)
listViewHolder.details_layout.setVisibility(View.GONE);
else listViewHolder.details_layout.setVisibility(View.VISIBLE);
*/
}
});
listViewHolder.coupon_code.setText(couponList.get(i));
return view;
}
最后在适配器类中创建一个holder类
public class Holder{
TextView coupon_description,coupon_percentage,coupon_code,details,limitations;
RelativeLayout details_layout;
}
阅读listview和适配器的一些概念。适配器如何工作。
推荐阅读
- android长textview封面imageview
- 使用android中的颜色选择器更改textview的文本颜色和背景颜色
- 计算百分比并在textview android studio中显示
- AWSAppSync GraphQL突变执行两次
- 为什么webview头键在android中以小写形式转换()
- Xamarin Android - Task.Run vs Task.Factory.StartNew和Thread.CurrentPrincipal
- 在VS 2017 xamarin android项目中没有击中断点
- 同步/阻止Application.Invoke()for GTK#
- 如何在Cordova中检索应用程序版本,应用程序名称,程序包名称,版本代码和版本号