码迷,mamicode.com
首页 > 其他好文 > 详细

仿酷狗音乐列表点击item子控件展开功能

时间:2016-05-12 22:22:07      阅读:411      评论:0      收藏:0      [点我收藏+]

标签:

也是项目需要大家先来看看效果图

技术分享

就是这样不过我的项目可能下面没有这么多项而已,说一下思路:

实际上下面展开的哪一块是被隐藏起来了的,可以获取itme的事件点击,接收点击事件展开(显示出来),点击另外的隐藏起来

下面是我的效果图(大家可以自己设置样式,我的比较丑)

技术分享

好图片有点大,但是看的爽对不!下面贴代码


public class MainActivity extends Activity  {
	private ListView mListView;
	private ListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mListView = (ListView) findViewById(R.id.list);
		mAdapter = new ListAdapter(this, listener, listener2, listener3);
		mListView.setAdapter(mAdapter);
		//mListView.setOnItemClickListener(this);
	}
	// @Override
	// public void onItemClick(AdapterView<?> parent, View view, int position,
	// long id) {
	// mAdapter.changeImageVisable(position);
	// }

	MyClickListener listener = new MyClickListener() {
		@Override
		public void myOnClick(int position, View v) {
			Toast.makeText(MainActivity.this, "修改", 2000).show();
		}
	};

	MyClickListener2 listener2 = new MyClickListener2() {
		public void myOnClick2(int position, View v) {

			Toast.makeText(MainActivity.this, "重置", 2000).show();
		}
	};

	MyClickListener3 listener3 = new MyClickListener3() {

		@Override
		public void myOnClick3(int position, View v) {
			mAdapter.changeImageVisable(position);
		}
	};
}

Activity的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

适配器代码:

public class ListAdapter extends BaseAdapter {
	private Context mContext;
	private int mLastPosition = -1;
	private MyClickListener mListener;
	private MyClickListener2 mListener2;
	private MyClickListener3 mListener3;

	public ListAdapter(Context context, MyClickListener mListener,
			MyClickListener2 mListener2, MyClickListener3 mListener3) {
		this.mContext = context;
		this.mListener = mListener;
		this.mListener2 = mListener2;
		this.mListener3 = mListener3;
	}

	@Override
	public int getCount() {
		return 100;
	}

	@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) {
		Holder holder = new Holder();
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.list_item, null);
			holder.textView = (TextView) convertView
					.findViewById(R.id.textView);
			holder.UEFAView = (ImageView) convertView
					.findViewById(R.id.image_uefa);
			holder.mascotView = (ImageView) convertView
					.findViewById(R.id.image_mascot);
			holder.hint = convertView.findViewById(R.id.hint_image);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.textView.setText("Hello,It is " + position);
		if (position == mLastPosition) {
			holder.hint.setVisibility(View.VISIBLE);
		} else {
			holder.hint.setVisibility(View.GONE);
		}

		holder.UEFAView.setOnClickListener(mListener);
		holder.UEFAView.setTag(position);
		//
		holder.mascotView.setOnClickListener(mListener2);
		holder.mascotView.setTag(position);

		holder.textView.setOnClickListener(mListener3);
		holder.textView.setTag(position);

		return convertView;
	}

	public static abstract class MyClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			myOnClick((Integer) v.getTag(), v);
		}

		public abstract void myOnClick(int position, View v);
	}

	public static abstract class MyClickListener2 implements OnClickListener {
		public void onClick(View v) {
			myOnClick2((Integer) v.getTag(), v);
		}

		public abstract void myOnClick2(int position, View v);
	}

	public static abstract class MyClickListener3 implements OnClickListener {
		public void onClick(View v) {
			myOnClick3((Integer) v.getTag(), v);
		}
		public abstract void myOnClick3(int position, View v);
	}

	class Holder {
		TextView textView;
		ImageView UEFAView;
		ImageView mascotView;
		View hint;
	}

	public void changeImageVisable(int position) {
		if (position != mLastPosition) {
			mLastPosition = position;
		} else {
			mLastPosition = -1;
		}
		notifyDataSetChanged();
	}
}

listview的item的list_item.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="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp" >

        <ImageView
            android:id="@+id/iv_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:src="@drawable/normal" />

        <LinearLayout
            android:id="@+id/ll_context"
            android:layout_width="200dp"
            android:layout_height="60dp"
            android:layout_toRightOf="@id/iv_status"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_context"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asdf" />

            <LinearLayout
                android:layout_width="150dp"
                android:layout_height="60dp"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tv_select"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="asdf" />

                <TextView
                    android:id="@+id/tv_setlen"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="asdf" />
            </LinearLayout>
        </LinearLayout>

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:text="展开"
            android:textSize="20sp" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/hint_image"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/image_uefa"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:src="@drawable/uefa" />

        <ImageView
            android:id="@+id/image_mascot"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:src="@drawable/mascot" />
    </LinearLayout>

</LinearLayout>

嗯!到这里就差不多了 我会把代码上传上去有兴趣的同志可以看看





仿酷狗音乐列表点击item子控件展开功能

标签:

原文地址:http://blog.csdn.net/qq_15338105/article/details/51353112

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!