Skip to content

Commit 2df29e4

Browse files
author
hongyangAndroid
committed
add listview demo
1 parent 2f4a4df commit 2df29e4

File tree

8 files changed

+164
-3
lines changed

8 files changed

+164
-3
lines changed

sample/android-percent-support/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
android:name=".ItemActivity"
2121
android:label="@string/title_activity_item" >
2222
</activity>
23+
<activity
24+
android:name=".ListViewTestActivity"
25+
android:label="@string/title_activity_list_view_test" >
26+
</activity>
2327
</application>
2428

2529
</manifest>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.imooc.android_percent_support;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.view.LayoutInflater;
6+
import android.view.Menu;
7+
import android.view.MenuItem;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.widget.ArrayAdapter;
11+
import android.widget.ListView;
12+
import android.widget.TextView;
13+
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
/**
18+
* 对于ListView,必须将item最外层的高度设置为固定值,
19+
* 没有办法使用百分比,因为其父控件是ListView,不是Percent系列容器;
20+
* 该Item内部的控件,可以使用percent系类属性;
21+
*/
22+
public class ListViewTestActivity extends AppCompatActivity
23+
{
24+
private ListView mListView;
25+
private LayoutInflater mInflater;
26+
27+
private List<String> mStr = Arrays.asList("Helloworld", "盗墓笔记", "秦时明月", "嫌疑人", "辩护人");
28+
29+
@Override
30+
protected void onCreate(Bundle savedInstanceState)
31+
{
32+
super.onCreate(savedInstanceState);
33+
setContentView(R.layout.activity_list_view_test);
34+
35+
mInflater = LayoutInflater.from(this);
36+
37+
mListView = (ListView) findViewById(R.id.id_listview);
38+
mListView.setAdapter(new ArrayAdapter<String>(this, -1, mStr)
39+
{
40+
@Override
41+
public View getView(int position, View convertView, ViewGroup parent)
42+
{
43+
if (convertView == null)
44+
{
45+
convertView = mInflater.inflate(R.layout.item_music_list, parent, false);
46+
}
47+
48+
TextView tv = (TextView) convertView.findViewById(R.id.id_tv_name);
49+
tv.setText(mStr.get(position));
50+
return convertView;
51+
}
52+
});
53+
54+
}
55+
56+
@Override
57+
public boolean onCreateOptionsMenu(Menu menu)
58+
{
59+
// Inflate the menu; this adds items to the action bar if it is present.
60+
getMenuInflater().inflate(R.menu.menu_list_view_test, menu);
61+
return true;
62+
}
63+
64+
@Override
65+
public boolean onOptionsItemSelected(MenuItem item)
66+
{
67+
// Handle action bar item clicks here. The action bar will
68+
// automatically handle clicks on the Home/Up button, so long
69+
// as you specify a parent activity in AndroidManifest.xml.
70+
int id = item.getItemId();
71+
72+
//noinspection SimplifiableIfStatement
73+
if (id == R.id.action_settings)
74+
{
75+
return true;
76+
}
77+
78+
return super.onOptionsItemSelected(item);
79+
}
80+
}

sample/android-percent-support/src/main/java/com/imooc/android_percent_support/MainActivity.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,25 @@ public class MainActivity extends ActionBarActivity
1919
private ListView mListView;
2020
private LayoutInflater mInflater;
2121

22+
private static final int LISTVIEW_POS = 6;
23+
2224
private String[] mTitle = {
2325
"PercentLinearLayout",
2426
"PercentW or PercentH",
2527
"PercentRelativeLayout 1",
2628
"PercentFrameLayout",
2729
"PercentRelativeLayout 2",
28-
"PercentLinearLayout in ScrollView"};
30+
"PercentLinearLayout in ScrollView",
31+
"PercentInListView"
32+
};
2933
private int[] mContentIds = {
3034
R.layout.view5,
3135
R.layout.view1,
3236
R.layout.view2,
3337
R.layout.view3,
3438
R.layout.view4,
35-
R.layout.view6
39+
R.layout.view6,
40+
-1
3641
};
3742

3843

@@ -65,6 +70,15 @@ public View getView(int position, View convertView, ViewGroup parent)
6570
@Override
6671
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
6772
{
73+
switch (position)
74+
{
75+
case LISTVIEW_POS:
76+
Intent intent = new Intent(MainActivity.this, ListViewTestActivity.class) ;
77+
startActivity(intent);
78+
return ;
79+
}
80+
81+
6882
Intent intent = new Intent(MainActivity.this, ItemActivity.class);
6983
intent.putExtra("contentId", mContentIds[position]);
7084
intent.putExtra("title",mTitle[position]);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<ListView android:id="@+id/id_listview"
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:paddingBottom="@dimen/activity_vertical_margin"
7+
android:paddingLeft="@dimen/activity_horizontal_margin"
8+
android:paddingRight="@dimen/activity_horizontal_margin"
9+
android:paddingTop="@dimen/activity_vertical_margin"
10+
tools:context="com.imooc.android_percent_support.ListViewTestActivity">
11+
12+
</ListView>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.zhy.android.percent.support.PercentRelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="56dp"
7+
>
8+
9+
<ImageView android:id="@+id/id_iv_icon"
10+
android:layout_width="0dp"
11+
android:layout_height="0dp"
12+
android:layout_centerVertical="true"
13+
android:layout_marginLeft="8dp"
14+
android:layout_marginRight="16dp"
15+
android:src="@mipmap/ic_launcher"
16+
app:layout_heightPercent="60%h"
17+
app:layout_widthPercent="60%h"/>
18+
19+
<TextView android:id="@+id/id_tv_name"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_alignTop="@+id/id_iv_icon"
23+
android:layout_toRightOf="@+id/id_iv_icon"
24+
android:text="Helloworld"
25+
android:textColor="#ff000000"
26+
app:layout_textSizePercent="26%h"
27+
/>
28+
29+
30+
<TextView android:id="@+id/id_tv_artist"
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:layout_alignBottom="@+id/id_iv_icon"
34+
android:layout_toRightOf="@+id/id_iv_icon"
35+
android:text="Helloworld"
36+
android:textColor="#66000000"
37+
app:layout_textSizePercent="24%h"
38+
/>
39+
40+
41+
</com.zhy.android.percent.support.PercentRelativeLayout>

sample/android-percent-support/src/main/res/layout/view3.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
android:layout_gravity="center"
4646
android:scaleType="centerCrop"
4747
android:src="@drawable/tangyan"
48-
app:layout_heightPercent="50%w"
48+
app:layout_heightPercent="50%h"
4949
app:layout_widthPercent="50%w"/>
5050

5151
<TextView
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
tools:context="com.imooc.android_percent_support.ListViewTestActivity">
5+
<item android:id="@+id/action_settings"
6+
android:title="@string/action_settings"
7+
android:orderInCategory="100"
8+
app:showAsAction="never"/>
9+
</menu>

sample/android-percent-support/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<string name="hello_world">Hello world!</string>
55
<string name="action_settings">Settings</string>
66
<string name="title_activity_item">ItemActivity</string>
7+
<string name="title_activity_list_view_test">ListViewTestActivity</string>
78
</resources>

0 commit comments

Comments
 (0)