Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> activity+fragment+listview+adapter+bean在同一個類中的套路,fragmentlistview

activity+fragment+listview+adapter+bean在同一個類中的套路,fragmentlistview

編輯:關於android開發

activity+fragment+listview+adapter+bean在同一個類中的套路,fragmentlistview


1.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</FrameLayout>

fargment_main.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="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"></ListView>
</LinearLayout>

list_item_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" >

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TEXT1"
android:textSize="16sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/text1"
android:layout_below="@+id/text1"
android:text="TEXT2"
android:textSize="12sp" />

</RelativeLayout>

2.java

MainActivity.java

public class MainActivity extends FragmentActivity {
MapView mMapView = null;
private BaiduMap mBaiduMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各組件之前初始化context信息,傳入ApplicationContext
//注意該方法要再setContentView方法之前實現
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}

}

public static class PlaceholderFragment extends Fragment implements AdapterView.OnItemClickListener {
private ListView listView;
private MainAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
listView= (ListView) view.findViewById(R.id.list);
adapter = new MainAdapter(getContext(),R.layout.list_item_main,getDateBean());
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return view;
}
public static List<DataBean> getDateBean() {
List<DataBean> list = new ArrayList<>();
list.add(new DataBean("基本地圖功能", "創建一張百度地圖並管理地圖的生命周期"));
list.add(new DataBean("圖層展示", "展示普通圖、衛星圖、交通流量圖及百度城市熱力圖"));
list.add(new DataBean("地圖操作功能", "介紹地圖基本控制方法"));
return list;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent;
switch (position) {
case 0:
intent = new Intent(getActivity(), BaseMap.class);
startActivity(intent);
break;
case 1:
intent = new Intent(getActivity(), Layers.class);
startActivity(intent);
break;
case 2:
intent = new Intent(getActivity(), Location.class);
startActivity(intent);
break;
default:
break;
}
}
}

public static class MainAdapter extends ArrayAdapter<DataBean> {
private int resourceID;
public MainAdapter(Context context, int resource, List<DataBean> objects) {
super(context, resource, objects);
resourceID = resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(getContext(), resourceID, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DataBean dataBean = getItem(position);
holder.text1.setText(dataBean.text1);
holder.text2.setText(dataBean.text2);
return convertView;
}

class ViewHolder{
TextView text1;
TextView text2;
}
}
public static class DataBean {
String text1;
String text2;

public DataBean(String text1, String text2) {
this.text1 = text1;
this.text2 = text2;
}
}

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved