将小部件向右或向左滑动

时间:2012-10-29 09:25:26

标签: android

我有ListView有几个LinearLayout类型的元素。如何像在联系人中一样制作幻灯片效果(当右边的联系人幻灯片有呼叫时,在另一种情况下写入消息)。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

那是你谈论的视图寻呼机 这是我为你做的一个例子

ViewPageExample.java文件

public class ViewPageExample extends Activity {

private ViewPager mViewPager;
private Context mContext;
private MyPageAdatper mAdapter;


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

mAdapter = new MyPageAdatper();
mViewPager = (ViewPager) findViewById(R.id.mViewPager);
mViewPager.setAdapter(mAdapter);
}

private class MyPageAdatper extends PagerAdapter{


        @Override
        public int getCount() {
               //Since only contacts messages and call logs were mentioned by you
                return 3;
        }


        @Override
        public Object instantiateItem(View collection, int position) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View mView = null ;
            switch(position){
                case 0:
                    mView   =   inflater.inflate(R.layout.contacts, null);  
                    Button btn1 = (Button) mView.findViewById(R.id.button1);
                    btn1.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
                        }
                    });
                break;

                case 1:
                    mView   =   inflater.inflate(R.layout.messages, null);   
                break;

                case 2:
                    mView   =   inflater.inflate(R.layout.callogs, null);   
                break;
            }




                ((ViewPager) collection).addView(mView,0);

                return mView;
        }


        @Override
        public void destroyItem(View collection, int position, Object view) {
                ((ViewPager) collection).removeView((View) view);
        }



        @Override
        public boolean isViewFromObject(View view, Object object) {
                return view==((View)object);
        }


        @Override
        public void finishUpdate(View arg0) {}


        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {}

        @Override
        public Parcelable saveState() {
                return null;
        }

        @Override
        public void startUpdate(View arg0) {}

}


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#a4c639">
<android.support.v4.view.ViewPager
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/mViewPager"/>
</LinearLayout>

callogs.xml

<?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:orientation="vertical" >


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Call Logs"
    android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

contacts.xml

<?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:orientation="vertical" >



<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Contacts"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

messages.xml

<?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:orientation="vertical" >



<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SomeMessages"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>