我正在创建一个包含不同标签的应用程序,幸运的是我已经在Android Studio中使用了Tabbed Activity Preset,并且我为三个不同的标签配置了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;
}
}
}
答案 0 :(得分:0)
使用自定义ArrayAdapter
时,您需要通过覆盖getView(int position, View convertView, ViewGroup parent)
方法传递自己的模板来呈现行。有关详细示例,请参阅this。