目前我正在尝试为我的应用程序创建一个自定义列表视图,我已经成功了。但我遇到的问题是,如果我将列表视图放在我的主活动上,那么列表视图将按计划显示文本和图像。但是使用我的应用程序,我可以在页面之间滑动,而不是在我的主活动页面上永久保存此列表视图,我希望它在片段页面上,例如WineList页面,当用户滑动到下一页时,您可以更长时间地看到它
如果我尝试将列表视图更改为片段页面上的视图,那么我会得到一个空的异常错误,这是可以理解的,因为我猜测在用户滑过之前页面尚未生成。
我已将下面的图片包含在内,以证明我的意思。
我的应用程序设置
自定义适配器类 -
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, String[] names) {
super(context, R.layout.custom_row, names);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater hadleighsInflater = LayoutInflater.from(getContext());
View customView = hadleighsInflater.inflate(R.layout.custom_row, parent, false);
String singleName = getItem(position);
TextView hadleighText = (TextView) customView.findViewById(R.id.hadleighText);
ImageView hadleighImage = (ImageView) customView.findViewById(R.id.myimageView);
hadleighText.setText(singleName);
hadleighImage.setImageResource(R.drawable.imageresize);
return customView;
}
}
主要活动 -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainwindow);
viewPager = (ViewPager)findViewById(R.id.swipeView);
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));
// Currently set up to the list view on the main activity
String[] names = {"Hadleigh", "james", "LOLLLO"};
ListAdapter hadleighsAdapter = new socialdeveloper.adgestonevineyard.CustomAdapter(this,names);
ListView hadleighsListView = (ListView)findViewById(R.id.myListview);
hadleighsListView.setAdapter(hadleighsAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabSwipe);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
WineList片段(我希望在其上显示我的列表视图)-----
public class WineList extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.winelist, container, false);
}
}
所以基本上它当前在主要活动上,但我不知道我必须实现哪些更改才能在片段页面上显示它。
答案 0 :(得分:0)
如果您想在(define (replace-after a x y l)
(cond
((< (length l) 2) l)
((and (equal? (car l) a)
(equal? (cadr l) x))
(cons (car l) (cons y (replace-after a x y (cddr l)))))
(else (cons (car l) (replace-after a x y (cdr l))))))
中显示Fragment
- 请使用FragmentStatePagerAdapter。
为ViewPager
创建ListView
版面。
使用一些参数为Fragment
的新实例创建静态方法。使用此参数,您可以在当前Fragment
中将适配器设置为ListView
的不同值。
如果您感到复杂,请按tutorial
只需将Fragment
替换为ImageView
,然后执行上述想法。
答案 1 :(得分:0)
你也可以使用这个完整的代码!它适用于我在示例图像中看到的内容。
首先,将其添加到build.gradle
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-annotations:23.1.1'
<强> WineListActivity 强>
//imports
import android.os.Bundle;
import android.support.design.widget.TabLayout;
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.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class WineListActivity extends AppCompatActivity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_winelist);
viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
viewPager.setOffscreenPageLimit(3);
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.viewpager);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new InformationFragment(), "INFORMATION");
adapter.addFragment(new HomeWebsitesFragment(), "LISTVIEW");
adapter.addFragment(new HomeFragment(), "HOME");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}
Xml布局, activity_winelist
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bdc3c7"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
您的列表片段, ListFragment
public class ListFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static ListFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
ListFragment fragment = new ListFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listview = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "wine 1", "wine 2", "wine 3",
"wine 4" };
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, list);
if(list.size() == 0){
listview.setEmptyView(findViewById(R.id.emptyview));
}
listview.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
return view;
}
}
您的列表list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
</RelativeLayout>
您的适配器 MySimpleArrayAdapter
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final List<String> values;
public MySimpleArrayAdapter(Context context, List<String> values) {
super(context, -1, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values.get(position));
return rowView;
}
}
然后你可以添加另外两个片段并随心所欲地做它们。
<强> HomeFragment 强>
public class HomeFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static HomeFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
HomeFragment fragment = new HomeFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
return view;
}
}
<强> InformationFragment 强>
public class InformationFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static InformationFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
InformationFragment fragment = new InformationFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_information, container, false);
return view;
}
}