繁体   English   中英

如何在Fragment Android Studio中显示ListView

[英]How to display a ListView in a Fragment Android Studio

我当时正在创建一个带有不同选项卡的应用程序,幸运的是,我在Android Studio中使用了“选项卡式活动预设”,并且为这三个不同的选项卡配置了3个片段。 但是,当我想显示一个包含标题,图像和描述的列表视图时,我发现遇到了一些错误。

好吧,我想要的是显示列表视图,我的列表确实在Strings.xml文件中,而且我不知道该怎么做才能使其工作。

这是我的片段Java文件:

package com.paradoxygo.guideforwwe2k;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by yugio on 04/03/2017.
 */

public class MatchType extends Fragment {

String [] theTitles;
String [] theDesc;
int [] images = {R.drawable.wide_cover_thumb};

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

View rootView = inflater.inflate(R.layout.matchtype_tab, container,
            false);
ListView listView = (ListView) rootView.findViewById(R.id.Guide);

Resources r = getResources();
theTitles = r.getStringArray(R.array.titles);
theDesc = r.getStringArray(R.array.Desc);

MyAdapter adapter = new MyAdapter(this, theTitles, images, theDesc);
    listView.setAdapter(adapter);





return rootView;
}

class MyAdapter extends ArrayAdapter <String> {

public MyAdapter (MatchType c, String[] titles, int[] imgs, String[] Desc ) {

super(c, R.layout.single_row, R.id.text1, titles);
    }

}
}

这是我的MainActivity Java文件:

package com.paradoxygo.guideforwwe2k;

import android.content.Context;
import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;



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


Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new           SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}


@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);
}


/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    private String[] tabTitles = new String[]{"Guide", "Match Type", "Tricks"};
        public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:

                GuideTab guideTab = new GuideTab();
                return guideTab;
            case 1:
                MatchType matchType = new MatchType();
                return matchType;
            case 2:
                Tricks tricks = new Tricks();
                return tricks;
        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }


}



}

当使用自定义ArrayAdapter您需要通过覆盖getView(int position, View convertView, ViewGroup parent)来传递自己的模板来呈现行。有关详细示例,请参考方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM