1. layout xml
<RelativeLayout 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" tools:context=".MainActivity"> <android.support.v4.widget.SlidingPaneLayout android:id="@+id/spl_sliding_pane" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv_sliding_list" android:layout_width="300dp" android:layout_height="match_parent" android:background="#CCCCCC"></ListView> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Open" /> <Button android:text="button" android:gravity="right" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv_open" /> </RelativeLayout> </android.support.v4.widget.SlidingPaneLayout> </RelativeLayout>
2. list item
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_sliding_list_item" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
3. java code
package com.torv.lijian.slidingpanelayoutdemo; import android.app.Activity; import android.os.Bundle; import android.support.v4.widget.SlidingPaneLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { SlidingPaneLayout mSlidingPaneLayout; private TextView mBtnOpen; private ListView mLvSliding; List<String> mListData; LayoutInflater inflater = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inflater = LayoutInflater.from(MainActivity.this); mSlidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.spl_sliding_pane); mBtnOpen = (TextView) findViewById(R.id.tv_open); mBtnOpen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mSlidingPaneLayout.openPane(); } }); mLvSliding = (ListView)findViewById(R.id.lv_sliding_list); mListData = new ArrayList<>(); mListData.add("This is Item 1"); mListData.add("This is Item 2"); mListData.add("This is Item 3"); mListData.add("This is Item 4"); MyAdapter adapter = new MyAdapter(); mLvSliding.setAdapter(adapter); mLvSliding.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "item " + position, Toast.LENGTH_SHORT).show(); } }); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return mListData.size(); } @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) { TextView tv = null; if(null == convertView){ convertView = inflater.inflate(R.layout.sliding_list_item, null); tv = (TextView)convertView.findViewById(R.id.tv_sliding_list_item); }else{ tv = (TextView)convertView.findViewById(R.id.tv_sliding_list_item); } tv.setText(mListData.get(position)); return convertView; } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-03 05:30:57