メインリストビューの別のアイテムをクリックすると、メインリストビューからフラグメントを別のdifferntリストビューフラグメントにコールしようとしています。main listviewのリストアイテムをクリックして、メインのlistviewフラグメントから他の異なるリストビューフラグメントに移動する方法
私は次のコード書かれていることについては:
私の主なリストビューフラグメントShopFragment.java: (私は別の項目をクリックすることで、別の断片を呼びたい、このリストから項目)
package fragments.h.safmical;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import safmical.h.R;
/**
* Created by hadvani on 4/13/2017.
*/
public class ShopFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.fragment_shop,container,false);
String[] list1={"x","y","z"};
ListView listView= (ListView) rootview.findViewById(R.id.mainlist);
ArrayAdapter<String> listviewadapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list1);
listView.setAdapter(listviewadapter);
FragmentManager fm =getFragmentManager();
listView.setOnClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
fm.beginTransaction().replace(R.id.content_frame,new ListOneFragment()).commit();
}
if(position==1){
fm.beginTransaction().replace(R.id.content_frame,new ListTwoFragment()).commit();
}
if(position==2){fm.beginTransaction().replace(R.id.content_frame,new ListThreeFragment()).commit();}
}
});
return rootview;
}
}
メインリストビューフラグメントxmlファイルfragment_shop.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#e6e6fa">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainlist"/>
</FrameLayout>
ListOneFragment.java:
package fragments.h.safmical;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import safmical.h.R;
/**
* Created by hadvani on 4/14/2017.
*/
public class ListOneFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_listone, container, false);
String[] list1 = {"a", "b", "c"};
ListView listView = (ListView) rootview.findViewById(R.id.list1);
ArrayAdapter<String> listviewadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list1);
listView.setAdapter(listviewadapter);
return rootview;
}
}
fragment_listone.xml:私はListThreeFragment.javaListTwoFragment.java、、fragment_listtwo.xmlを実装している
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/list1">
</ListView>
</FrameLayout>
同じよう and fragment_listthree.xml。
でも機能しません。 Gradle buildに次のエラーが表示されます:
これを行うにはどうすればよいですか、それとも他の方法がありますか? これを解決するために私を助けてください。あなたは何が起こっている
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
に
listView.setOnClickListener(new AdapterView.OnItemClickListener() {
を変更する必要が
葉、私はそれを変更しているし、私は最終的。だからエラーsolved.Thanks – harsh