需要android.support.v4.app.fragment

时间:2016-08-26 22:25:04

标签: java android listview android-fragments

我有一个包含列表视图的简单项目 当任何人点击列表视图上的任何项目时 在单一窗口打开 通过片段 问题是我想用这个项目上的这个标签片段替换我的片段 http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/

我把我的mainactivty类放在switch case中,但它找到了我这个错误 在第一张照片中 我尝试导入导入android.support.v4.app.Fragment 但也是同样的问题

这是我项目的内容 .... Main2Activity类

public class Main2Activity extends AppCompatActivity implements fragmentA.comunicatir
        {

        fragmentA f1;
        fragmentB f2;
        FragmentManager manegaer;

        @Override
        protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

                manegaer=getSupportFragmentManager();
        f1=(fragmentA)manegaer.findFragmentById(R.id.fragment1);
        f1.setcomunicatir(this);
        }


        @Override
        public void respned(int index){
                f2=(fragmentB)manegaer.findFragmentById(R.id.fragment2);

        if(f2!=null&&f2.isVisible()){

        f2.cahngedata(index);
        }else{
        Intent intent=
        new Intent(this,
        AnotherActivity.class);
        intent.putExtra("index",index);
        startActivity(intent);
        }


        }
        }

error 2

另一个班级 class fragmentA

 public class fragmentA extends Fragment implements AdapterView.OnItemClickListener
{
    ListView list;
    comunicatir  com;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment1,container,false);
        list=(ListView)view.findViewById(R.id.listView1);

        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.chapters, android.R.layout.simple_list_item_1);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
        return  view;

    }

    public void setcomunicatir (comunicatir comunicator)
    {

        this.com=comunicator;
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View veiw, int i, long l) {

        com.respned(i);

    }


    public interface comunicatir
    {
        public void respned(int index);

    }
}

    public class fragmentB  extends Fragment

{

    TextView text;
    TextView textView2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment2, container, false);
        //text = (TextView) view.findViewById(R.id.textView1);
        //imgv=(ImageView) view.findViewById(R.id.imageView1);
        textView2 =(TextView)view.findViewById(R.id.textView22);

        return view;

    }
    public void cahngedata(int index)
    {

    /*String[] descrption =getResources().getStringArray(R.array.hadeith);
            text.setText(descrption[index]);*/

        String []Hadith=getResources().getStringArray(R.array.hadeith);
        //imgv.setImageResource(img[index]);
        textView2.setText(Hadith[index]);

        //int []descrption =getResources().getIntArray(img[index]);
        //  imgv.setImageResource(descrption[index]);       

            /*text.setText(descrption[index]);
        int arr[]=getResources().getIntArray(img[index]);
            imgv.setImageResource(arr[index]);*/


    }
}



public class AnotherActivity extends Activity

{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.another);

        Intent intent = getIntent();
        int index= intent.getIntExtra("index", 0);
        fragmentB f2= (fragmentB)
                getFragmentManager().
                        findFragmentById(R.id.fragment2);
        if (f2!=null)
            f2.cahngedata(index);

    }
}

另一个Activity类在图片2中也有错误

error 2

2 个答案:

答案 0 :(得分:0)

第一期:您无法在Tab上放置活动,但您可能不需要getItem()可以删除该方法

第二期:

替换:

fragmentB f2 = (fragmentB) ...

使用:

fragment f2 = (Fragment) ...

答案 1 :(得分:0)

转到你遇到问题的tab.class并进行更改 import android.app.Fragment; 至 import android.support.v4.app.Fragment;

它会运行。