自定义的课折叠的LinearLayout,啥也不说了,上图上代码
这种可折叠的LinearLayout,内容可插入任何布局和任何形式的数据,不说了上代码,写的不好,勿喷
MainActivity
package mylinear.zhang.com.mylinear;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements MyLinear.LinearListener{
private MyLinear item1,item2,item3,item4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
item1=(MyLinear)findViewById(R.id.order_info);
item2=(MyLinear)findViewById(R.id.order_borrowerInfo);
item3=(MyLinear)findViewById(R.id.order_pledge);
item4=(MyLinear)findViewById(R.id.order_check);
item1.init(R.layout.item1,"Item1",this);
item2.init(R.layout.item2,"Item2",this);
item3.init(R.layout.item3,"Item3",this);
item4.init(R.layout.item4,"Item4",this);
}
@Override
public void setData(View view, Object datas) {
switch (view.getId()){
case R.id.item1:
Log.e("LinearData","order_info content: "+String.valueOf(datas));
TextView textView1=(TextView)view.findViewById(R.id.tv_item1);
textView1.setText(String.valueOf(datas));
break;
case R.id.item2:
Log.e("LinearData","order_borrowerInfo content :"+String.valueOf(datas));
TextView textView2=(TextView)view.findViewById(R.id.tv_item2);
textView2.setText(String.valueOf(datas));
break;
case R.id.item3:
Log.e("LinearData","order_pledge content :"+String.valueOf(datas));
TextView textView3=(TextView)view.findViewById(R.id.tv_item3);
textView3.setText(String.valueOf(datas));
break;
case R.id.item4:
Log.e("LinearData","order_check content :"+String.valueOf(datas));
TextView textView4=(TextView)view.findViewById(R.id.tv_item4);
textView4.setText(String.valueOf(datas));
break;
}
}
}
MyLinearLayout
package mylinear.zhang.com.mylinear;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Administrator on 2016/10/14.
*/
public class MyLinear extends LinearLayout implements View.OnClickListener{
private View view, childview;
private LinearLayout content;
private TextView item;
private ImageView updown;
private Context mcontext;
private String text;
private LayoutParams params;
private Boolean flag = true;
private Object datas;
private LinearListener linearListener;
public MyLinear(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mcontext = context;
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.MyLinear, defStyleAttr, 0);
text = a.getString(R.styleable.MyLinear_lieartext);
a.recycle();
}
public MyLinear(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyLinear(Context context) {
super(context);
}
public void init(int layout,Object datas,LinearListener linstener) {
this.linearListener=linstener;
this.setOrientation(LinearLayout.HORIZONTAL);
this.setGravity(Gravity.CENTER_VERTICAL);
LayoutInflater inflater = LayoutInflater.from(mcontext);
params = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
view = inflater.inflate(R.layout.mylinear, null);
item = (TextView) view.findViewById(R.id.mylinear_item);
content = (LinearLayout) view.findViewById(R.id.linear_content);
updown = (ImageView) view.findViewById(R.id.linear_updown);
item.setText(text);
this.setOnClickListener(this);
inflater = LayoutInflater.from(mcontext);
childview = inflater.inflate(layout, null);
content.removeAllViews();
this.removeAllViews();
content.addView(childview);
if (linearListener==null){
Toast.makeText(getContext(),"linearListener is Null",Toast.LENGTH_LONG).show();
Log.e("MyLinear","linearListener is Null");
}else {
linearListener.setData(childview,datas);
}
this.addView(view, params);
}
private void isShowChild() {
if (flag) {
content.setVisibility(View.VISIBLE);
updown.setImageResource(R.mipmap.up);
flag=false;
} else {
content.setVisibility(View.GONE);
updown.setImageResource(R.mipmap.down);
flag=true;
}
}
@Override
public void onClick(View v) {
if (v instanceof MyLinear){
isShowChild();
}
}
interface LinearListener{
void setData(View view,Object datas);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="mylinear.zhang.com.mylinear.MainActivity">
<mylinear.zhang.com.mylinear.MyLinear
android:id="@+id/order_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:animateLayoutChanges="true"
app:lieartext="@string/bdxx" />
<mylinear.zhang.com.mylinear.MyLinear
android:id="@+id/order_borrowerInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:animateLayoutChanges="true"
app:lieartext="@string/jkrxx" />
<mylinear.zhang.com.mylinear.MyLinear
android:id="@+id/order_pledge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:animateLayoutChanges="true"
app:lieartext="@string/dywxx" />
<mylinear.zhang.com.mylinear.MyLinear
android:id="@+id/order_check"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:animateLayoutChanges="true"
app:lieartext="@string/shxx" />
</LinearLayout>
mylinear.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/mylinear_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/black"
android:textSize="14sp" />
<ImageView
android:id="@+id/linear_updown"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/up" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
android:paddingTop="1dp"
android:visibility="visible">
</LinearLayout>
</LinearLayout>
先关源码,有兴趣可以看