你的位置:首页 > 操作系统

[操作系统]activity+fragment+listview+adapter+bean在同一个类中的套路


1.

activity_main.

<?<FrameLayout     android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</FrameLayout>

fargment_main.

<?<LinearLayout   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.

<?<RelativeLayout   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;
}
}

}