Android ListView点击item 显示隐匿菜单项
java哥
阅读:643
2021-03-31 23:31:45
评论:0
解决思路:
Demo:
package com.activity.gdbk;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class ListActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {
//自定义相关组件
private ListView listview;
private MyAdapter adapter;
//activity 数据源
private List<String> data;
//activity 标量
private int currentPosition = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//全屏模式
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_listview);
initView();
setData();
}
//自定义相关方法
private void initView() {
listview = (ListView) findViewById(R.id.listview);
listview.setOnItemClickListener(this);
listview.setOnItemLongClickListener(this);
}
private void setData() {
data = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
data.add("员工姓名" + i);
}
if (adapter == null) {
adapter = new MyAdapter(this, data);
listview.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
@Override
protected void onResume() {
updateNoteBookList();
super.onResume();
}
private void updateNoteBookList() {
if (adapter != null) {
setData();
adapter.notifyDataSetChanged();
}
}
//覆写相关方法
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(40);
currentPosition = position;
adapter.notifyDataSetChanged();
return true;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
currentPosition = -1;
adapter.notifyDataSetChanged();
}
//内部类---自定义适配器
public class MyAdapter extends BaseAdapter{
private Context context;
private LayoutInflater inflater;
private List<String> fileTypeList;
//适配器构造函数
public MyAdapter(Context context, List<String> fileTypeList) {
this.context = context;
this.fileTypeList = fileTypeList;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return fileTypeList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return fileTypeList.get(arg0);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview_item, parent, false);
holder = new ViewHolder();
holder.fileNameText = (TextView) convertView.findViewById(R.id.item_name);
holder.fileTimeText = (TextView) convertView.findViewById(R.id.item_time);
holder.fileSizeText = (TextView) convertView.findViewById(R.id.item_pm);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.layout_other);
holder.openLinearLayout = (LinearLayout) convertView.findViewById(R.id.item_open);
holder.editLinearLayout = (LinearLayout) convertView.findViewById(R.id.item_edit);
holder.moveLinearLayout = (LinearLayout) convertView.findViewById(R.id.item_move);
holder.deleteLinearLayout = (LinearLayout) convertView.findViewById(R.id.item_delete);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//填充相关数据信息
String strs = fileTypeList.get(position);
holder.fileNameText.setText(strs);
holder.fileTimeText.setText("");
holder.fileSizeText.setText("");
//item 显示隐藏菜单
if (position == currentPosition) {
holder.linearLayout.setVisibility(View.VISIBLE);
holder.openLinearLayout.setClickable(true);
holder.editLinearLayout.setClickable(true);
holder.moveLinearLayout.setClickable(true);
holder.deleteLinearLayout.setClickable(true);
holder.openLinearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//饼状图页面
Intent pie=new Intent(ListActivity.this,PieActivity.class);
startActivity(pie);
}
});
holder.editLinearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//柱状图页面
Intent intent=new Intent(ListActivity.this,BarActivity.class);
startActivity(intent);
}
});
holder.moveLinearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//散点图页面
Intent scatter=new Intent(ListActivity.this,ScatterAtivity.class);
startActivity(scatter);
}
});
holder.deleteLinearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
折线图页面
Intent linear=new Intent(ListActivity.this,LinearActivity.class);
startActivity(linear);
}
});
} else {
holder.linearLayout.setVisibility(View.GONE);
holder.openLinearLayout.setClickable(false);
holder.editLinearLayout.setClickable(false);
holder.moveLinearLayout.setClickable(false);
holder.deleteLinearLayout.setClickable(false);
}
return convertView;
}
}
//内部类-----自定义组件
public class ViewHolder{
public TextView fileNameText;
public TextView fileTimeText;
public TextView fileSizeText;
public LinearLayout linearLayout;
public LinearLayout openLinearLayout;
public LinearLayout editLinearLayout;
public LinearLayout moveLinearLayout;
public LinearLayout deleteLinearLayout;
}
}
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/list_selector"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/item_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:orientation="vertical" >
<TextView
android:id="@+id/item_name"
style="@style/note_name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:maxWidth="240dip"
android:textColor="#fff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:layout_marginRight="20dip"
android:layout_weight="1"
android:maxWidth="240dip"
android:textColor="#fff"
android:textSize="12sp" />
<TextView
android:id="@+id/item_pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginLeft="20dip"
android:layout_weight="1"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="5dip"
android:background="@drawable/group_arrow_right"
android:gravity="right|center_vertical" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_other"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#808080"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone" >
<LinearLayout
android:id="@+id/item_open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/list_selector1"
android:clickable="false"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/open_icon"
android:gravity="right|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="饼状图"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/item_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:background="@drawable/list_selector1"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/edit_icon"
android:gravity="right|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="柱状图"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/item_move"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:background="@drawable/list_selector1"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/move_icon"
android:gravity="right|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="散点图"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/item_delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:background="@drawable/list_selector1"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/delete_icon"
android:gravity="right|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="折线图"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
效果展示:
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。