带有导航抽屉的片段中的 ListView 和 ViewPager

Posted

技术标签:

【中文标题】带有导航抽屉的片段中的 ListView 和 ViewPager【英文标题】:ListView and ViewPager within Fragment with Navigation Drawer 【发布时间】:2015-04-26 20:25:36 【问题描述】:

我正在尝试将这两个功能都实现到片段中,但到目前为止还没有成功。它在ListViewViewPager 中都显示错误Cannot resolve method findViewById 也是Cannot resolve constructor ArrayAdapter(this, android.R.layout.simple_list_item_1, categoria) 我想做的是从我的IntroActivity.java 输入到FmMenu.java 并使用导航抽屉导航到我的另一个片段FmContact.java位于 MenuActivity.java 的单独类中。

这是我的 FmMenu.java

import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by WiLo on 2/13/2015.
 */
public class FmMenu extends Fragment 

    String[] categoria = 
            "Jeans"
    ;

    int[] imagenes = 
            R.drawable.veroxjeans1,
            R.drawable.veroxjeans2,
            R.drawable.veroxjeans3,
            R.drawable.veroxjeans4,
            R.drawable.veroxjeans5,
            R.drawable.veroxjeans6,
            R.drawable.veroxjeans7
    ;

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

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

        View rootView = inflater.inflate(R.layout.lay_menufragment, container, false);



        //lista
        ListView lista = (ListView) findViewById(R.id.listView1);
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, categoria);
        lista.setAdapter(adapter);

        //galeria de imagenes

        mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[0]));
        mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[1]));
        mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[2]));
        mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[3]));
        mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[4]));
        mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[5]));
        mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[6]));

        mViewPager.setAdapter(mSectionsPagerAdapter);

        return rootView;

    

    /**@Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_menufragment);


    **/

    public class SectionsPagerAdapter extends FragmentPagerAdapter 

        List<Fragment> fragmentos;
        public SectionsPagerAdapter(FragmentManager fm) 
            super(fm);
            fragmentos = new ArrayList<Fragment>();
        

        public void addfragments(Fragment xfragment)
            fragmentos.add(xfragment);
        


        @Override
        public Fragment getItem(int position) 
            return fragmentos.get(position);
        

        @Override
        public int getCount() 
            return fragmentos.size();
        
    


    public static class PlaceholderFragment extends Fragment 

        private static final String ARG_IMAGE = "imagen";
        private int imagen;

        public static PlaceholderFragment newInstance(int imagen) 
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_IMAGE, imagen);
            fragment.setArguments(args);
            fragment.setRetainInstance(true);
            return fragment;
        

        @Override
        public void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            if(getArguments() != null) 
                imagen = getArguments().getInt(ARG_IMAGE);
            
        

        public PlaceholderFragment() 
        

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) 
            View rootView = inflater.inflate(R.layout.fragment_menu, container, false);

            ImageView imagenView = (ImageView) rootView.findViewById(R.id.imageView1);
            imagenView.setImageResource(imagen);
            return rootView;
        
    


这是我的 IntroActivity.java

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

/**
 * Created by WiLo on 2/13/2015.
 */
public class IntroActivity extends Activity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        /*getActionBar().hide();*/
        setContentView(R.layout.activity_intro);
        Log.i("BunBunUp", "MainActivity Created");
    

    /**@Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_intro, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) 
            return true;
        

        return super.onOptionsItemSelected(item);
    **/

    public void startMenuActivity(View v)
        Intent intent = new Intent(IntroActivity.this, MenuActivity.class);
        startActivity(intent);
    

    protected void onResume()
        super.onResume();
        Log.i("BunBunUp", "IntroActivity Resumed");
    

    protected void onPause()
        super.onPause();
        Log.i("BunBunUp", "IntroActivity Paused");
    

    protected void onStop()
        super.onStop();
        Log.i("BunBunUp", "IntroActivity Stopped");
    

任何帮助将不胜感激

【问题讨论】:

【参考方案1】:

你需要引用它的View对象(rootView)来调用方法,如:

ListView lista = (ListView) rootView.findViewById(R.id.listView1);

我假设 lay_menufragment xml 包含 listView1 作为 ListView 元素的 ID。 用同样的方法修复其他类似的编译错误。

我看不出您是如何通过 Intent、布局或其他方式启动 FmMenu 片段的。请同时发布布局文件,如lay_menufragment,以节省时间。

【讨论】:

感谢您的帮助...需要添加rootView 不客气。根据您的问题,我终于注意到您可以在没有 View 对象的情况下在 MainActivity 中调用 findViewById,因为它实现了一个 View。

以上是关于带有导航抽屉的片段中的 ListView 和 ViewPager的主要内容,如果未能解决你的问题,请参考以下文章

带有片段的 Android Studio 导航抽屉。工具栏隐藏在下一个片段活动或页面中

带有 ListView 的导航抽屉。 ListView 上的空指针异常

带有许多片段的 Android 导航抽屉

带有活动和子片段的导航抽屉

Listview 项目未在 Fragment Android 中显示

带有嵌套片段的导航抽屉 (ViewPager)