Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在片段类中显示listView?_Java_Android_Listview_Android Fragments_Tabs - Fatal编程技术网

Java 如何在片段类中显示listView?

Java 如何在片段类中显示listView?,java,android,listview,android-fragments,tabs,Java,Android,Listview,Android Fragments,Tabs,我是android编程新手。我想尝试使用片段制作简单的选项卡布局应用程序,如何在片段中显示listView,我在谷歌上搜索并找到了一个解决方案,但它并没有使用fragment类。我可以显示webview,但如何显示listVIew 我的tab1类: public class Tab1 extends Fragment { WebView webView; @Override public void onActivityCreated(Bundle savedInst

我是android编程新手。我想尝试使用片段制作简单的选项卡布局应用程序,如何在片段中显示listView,我在谷歌上搜索并找到了一个解决方案,但它并没有使用fragment类。我可以显示webview,但如何显示listVIew

我的tab1类:

       public class Tab1 extends Fragment {
WebView webView;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // get the url to open
        // set up the WebView
        webView = (WebView) getView().findViewById(R.id.webView);
        webView.setWebViewClient(new MyBrowser());
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl("http://www.google.com");
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.tab_1,container,false);
        return v;
    }
    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
我的tab2类(我想在这里显示listView):

片段适配器:

公共类ViewPagerAdapter扩展了FragmentStatePagerAdapter{

CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created


// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
    super(fm);

    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;

}

//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        Tab1 tab1 = new Tab1();
        return tab1;
    }
    else   //if (position == 1)       // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
    {
        Tab2 tab2 = new Tab2();
        return tab2;
    } //        else //        { //            NewKasus newKasus = new NewKasus(); //            return newKasus; //        }

}

// This method return the titles for the Tabs in the Tab Strip

@Override
public CharSequence getPageTitle(int position) {
    return Titles[position];
}

// This method return the Number of tabs for the tabs Strip

@Override
public int getCount() {
    return NumbOfTabs;
} }
我的主要活动:

public class MainActivity extends ActionBarActivity {
//


    // Declaring Your View and Variables

    Toolbar toolbar;
    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    CharSequence Titles[]={"Berita","Kasus","Add Kasus"};
    int Numboftabs =3;


    //
   // bawaaan materal tab
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Creating The Toolbar and setting it as the Toolbar for the activity

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);


        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);



    }


    @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_main, 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 class Tab1 extends ListFragment { 
String[] list_items;    

Tab1(){
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView =inflater.inflate(R.layout.tab_1,container,false);
    list_items= getResources().getStringArray(R.array.list);
    setListAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,list_items);

 return rootView;
}
}
在您的strings.xml中

 <string-array name="list">
         <item>item1</item>
         <item>item1</item>
         <item>item1</item>
         <item>item1</item>
         <item>item1</item>
  </string-array>

项目1
项目1
项目1
项目1
项目1
您的tab_1布局必须具有..listView.with idandroid:list 只有在使用ListFragment类扩展片段时,才需要这样做。 如果您只是使用片段进行扩展,则无需添加此特定idandroid:list

对于这个例子,我给你需要以下在你的布局

<ListView android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FF00"
    android:layout_weight="1"
    />

这就是在片段中创建listView所需的全部内容

在您的例子中,您在数组中获取数据,然后在listView中传递它。
这里是我在本地使用的示例。希望有帮助!

您的数据源是什么?如果您使用的是普通字符串,您可以在您的
onCreateView
方法中执行此操作,就像在活动中执行此操作一样


如果您是从数据库或网络获取数据,最好使用AsyncTask的
onPostExecute
方法执行此操作,请添加一个特定的问题声明-“它不起作用”可以假设,但它是如何工作的?什么错误消息或错误行为是其特征?请查看此答案中的代码:
<ListView android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FF00"
    android:layout_weight="1"
    />