请告诉我当我点击listview时,我将如何进入下一页...继续下一个活动
import android.app.Fragment;
import android.os.Bundle;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView;
公共类FragMent1扩展Fragment {
private String arry[] = { "Tofeeq", "Ahmad", "Fragment", "Example",
"Tofeeq", "Ahmad", "Fragment", "Example" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView listView = new ListView(getActivity());
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1);
for (String str : arry)
array.add(str);
listView.setAdapter(array);
return listView;
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(getActivity(), NextActivity.class);
startActivity(myIntent);
}
});
return root;
}
}
答案 0 :(得分:1)
只需这样做:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
/* do what you want here, like changing fragment */
}
});
现在您可以使用它来加载下一页。
如果您在我的情况下,如果您使用相同的列表模板,我建议使用该捆绑包知道如何填写您的列表视图,以及要更改的监听器。
例如,如果我想访问所选国家/地区的州名单,我就可以访问包含某些国家/地区的列表视图的下一页:
Bundle bundle = new Bundle();
bundle.putString("type", "state");
bundle.putString("value", ((TextView) arg1).getText().toString());
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
AlphabeticalListViewFragment liste = new AlphabeticalListViewFragment();
liste.setArguments(bundle);
fragmentTransaction.replace(android.R.id.content, liste, "listfragment");
fragmentTransaction.addToBackStack(liste.getClass().getName());
fragmentTransaction.commit();
然后只需阅读您的捆绑包即可知道如何填写您的列表:
Bundle bundle = this.getArguments();
String typeListe= bundle.getString("type", "country");
if(typeListe.equals("country"))
{...
答案 1 :(得分:1)
如果您使用的是片段,则必须使用 getActivity()
代替CurrentActivity.this
import android.widget.AdapterView;
public class FragMent1 extends Fragment {
private String arry[] = { "Tofeeq", "Ahmad", "Fragment", "Example",
"Tofeeq", "Ahmad", "Fragment", "Example" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = (LinearLayout) inflater.inflate(R.layout.yourlayout, container, false);
ListView lv = (ListView)view.findViewById(R.id.listviewid);
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1);
for (String str : arry)
array.add(str);
lv.setAdapter(array);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(getActivity(), NextActivity.class);
startActivity(myIntent);
}
});
return root;
}
}
listviewid
是您在Layout中设置的ListView ID。 yourlayout
yourlayout.xml?
在清单文件中添加新活动:在<application>
标记内
<application>
<activity android:label="@string/app_name" android:name="NextActivity"/>
</application>
将此ListView添加到xml
<RelativeLayout
xmlns:android="schemas.android.com/apk/res/android";
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:id="@+id/yourlayout">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listviewid"
android:layout_gravity="center_horizontal"
android:choiceMode="singleChoice"
android:fastScrollEnabled="true"/>
</RelativeLayout>
答案 2 :(得分:0)
public class Fragment1 extends Fragment {
private String arry[] = { "Tofeeq", "Ahmad", "Fragment", "Example",
"Tofeeq", "Ahmad", "Fragment", "Example" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = (LinearLayout) inflater.inflate(R.layout.yourlayout, container, false);
ListView lv = (ListView)view.findViewById(R.id.listviewid);
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1);
for (String str : arry)
array.add(str);
lv.setAdapter(array);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
return view;
}
}
在清单文件中添加新活动:
<activity android:label="@string/app_name" android:name="NextActivity"/>
答案 3 :(得分:0)
您需要 ListView.OnItemClickListener
https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
实现侦听器并确定侦听器方法中的位置参数单击了哪个侦听器。