xml文件代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/image">
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="我是文字 要被隐藏" />
</LinearLayout>
</RelativeLayout>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher" />
</RelativeLayout>
java代码:
public class MainActivity extends AppCompatActivity {
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
}
public static int position;
public static boolean isShow = true;
@Override
protected void onStart() {
super.onStart();
myAdapter = new MyAdapter();
listview.setAdapter(myAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(MainActivity.position == position){
MainActivity.isShow = false;
MainActivity.position = 99999999;
}else{
MainActivity.isShow = true;
MainActivity.position = position;
}
int childCount = ((RelativeLayout) view).getChildCount();
if(myAdapter.getView() != null){
myAdapter.getView().setVisibility(View.GONE);
}
for (int i = 0; i < childCount; i++) {
int relativelayout = R.id.relativelayout; //要隐藏布局的外层一个容器ID
int id1 = ((RelativeLayout) view).getChildAt(i).getId();
if(relativelayout == id1){//如果是要隐藏布局的外层一个容器 就循环寻找要隐藏的容器
RelativeLayout childAt = (RelativeLayout) ((RelativeLayout) view).getChildAt(i);
int childCount1 = childAt.getChildCount();
for (int j = 0; j < childCount1; j++) {
int line1 = R.id.line1;
int id2 = childAt.getChildAt(j).getId();
if(line1 == id2){
if(MainActivity.isShow){
childAt.getChildAt(j).setVisibility(View.VISIBLE);
}else {
childAt.getChildAt(j).setVisibility(View.GONE);
}
if(childAt.getChildAt(j).getVisibility() == View.INVISIBLE){
Log.e("eee","出现这个日志,请检查你的类型转换,和xml结构是否符合当前的代码。");
}
myAdapter.setView((LinearLayout) childAt.getChildAt(j));
}
}
}
}
}
});
}
MyAdapter myAdapter;
class MyAdapter extends BaseAdapter {
LinearLayout view; //要隐藏或者展示的view ——如果是多个,可以修改为一个Entity
public LinearLayout getView(){
return view;
}
public void setView(LinearLayout view){
this.view = null;
this.view = view;
}
@Override
public int getCount() {
return 7;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
convertView = View.inflate(MainActivity.this, R.layout.item, null);
vh.image = (ImageView) convertView.findViewById(R.id.image);
vh.text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
return convertView;
}
class ViewHolder {
ImageView image;
TextView text;
}
}
}
耦合度很高,只要布局文件里面的结构改变,onItemClick的代码就要改。。。。
15:38更新一次:添加了,当点击同一个两次,第二次应该要GONE;