我使用的场景是我需要在本机android应用程序的ViewPager中查看主详细信息(列表详细信息)。请在下面找到我要遵循的方法。
TabBarActivity.java
import android.content.Intent;
import android.content.res.Configuration;
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 android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class TabBarActivity extends AppCompatActivity implements
LaunchDialogFragmentListener, HandleCallBack {
private static final String TAG = TabBarActivity.class.getSimpleName();
private SectionsPagerAdapter mSectionsPagerAdapter;
private static final String STATE_IS_FIRST_RUN = "STATE_IS_FIRST_RUN";
private boolean isFirstRun = true;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_bar);
if (savedInstanceState != null) {
isFirstRun = savedInstanceState.getBoolean(STATE_IS_FIRST_RUN);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
mSectionsPagerAdapter.addFragment(new MasterProfileFragment(),
Integer.valueOf(NavSectionProvider.POS_PROFILE));
mSectionsPagerAdapter.addFragment(new PestidFragment(),
Integer.valueOf(NavSectionProvider.POS_PEST_ID));
mSectionsPagerAdapter.addFragment(new AboutFragment(),
Integer.valueOf(NavSectionProvider.POS_ABOUT));
mViewPager = (ViewPager) findViewById(R.id.tab_bar_vp_container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new
ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
manageHomeButton(position);
manageTitle(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
for (NavSection section : NavSectionProvider.sections()) {
tabLayout.getTabAt(section.getPosition()).
setIcon(section.getImageResId());
}
}
@Override
public void didSelectTabPosition(int tabPosition) {
mViewPager.setCurrentItem(tabPosition);
}
public static final String IMAGE_ID = "image_id";
@Override
public void loadImage(int imagesrc) {
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE){
/*The below line is giving errror. detailFragment is getting null
reference. ProfileDetailFragment not able to read
R.id.profile_detail_fragment. */
ProfileDetailFragment detailFragment = (ProfileDetailFragment)
getSupportFragmentManager().
findFragmentById(R.id.profile_detail_fragment);
detailFragment.setImageView(imagesrc);
} else{
Intent intent = new Intent(this.getBaseContext(),
ProfileDetailActivity.class);
intent.putExtra(IMAGE_ID, imagesrc);
startActivity(intent);
}
}
private void manageHomeButton(int position) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false)
}
private void manageTitle(int position) {
NavSection section = NavSectionProvider.sections().get(position);
getSupportActionBar().setTitle(section.getTitle(this));
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<Integer> mFragmentTitleList = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, Integer title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public Fragment getItem(int position) {
NavSection section = NavSectionProvider.sections().get(position);
if (section.getPosition() == NavSectionProvider.POS_PROFILE) {
return mFragmentList.get(NavSectionProvider.POS_PROFILE);
} else if (section.getPosition() == NavSectionProvider.POS_ABOUT) {
return mFragmentList.get(NavSectionProvider.POS_ABOUT);
} else if (section.getPosition() ==
NavSectionProvider.POS_Applications) {
return mFragmentList.get(NavSectionProvider.POS_Applications);
} else {
return null;
}
}
@Override
public int getCount() {
return NavSectionProvider.sections().size();
}
@Override
public CharSequence getPageTitle(int position) {
return "";
}
}
}
我在引发异常的地方添加了注释。 *下面的线是错误的。 detailFragment正在获取空引用。 ProfileDetailFragment无法读取R.id.profile_detail_fragment。 * /
下面是activity_tab_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true"
tools:context="c.TabBarActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="@color/logo_grey"
app:tabMaxWidth="0dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/logo_grey"
app:tabTextColor="@color/white" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.design.widget.CoordinatorLayout>
下面是我的MasterProfileFragment.java类。
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MasterProfileFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_master_profile, container, false);
return view;
}
}
下面是我的Profile ListView片段。 ProfileFragment.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.
public class ProfileFragment extends Fragment implements AdapterView.OnItemClickListener {
private String[] imageName = new String[]{
"Image1", "Image2", "Image3", "Image4"
};
private int[] imagesFromRes = new int[]{
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7
};
private HandleCallBack handleCallBack;
private static final String SELECTED_POSITION = "selected";
private int position = -1;
private ListView imageListView;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof HandleCallBack) {
handleCallBack = (HandleCallBack) context;
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_profile_list, container, false);
imageListView = (ListView) viewGroup.findViewById(R.id.imagelist);
imageListView.setOnItemClickListener(this);
imageListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_activated_1, imageName);
imageListView.setAdapter(adapter);
return viewGroup;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null){
position = savedInstanceState.getInt(SELECTED_POSITION);
imageListView.setItemChecked(position, true);
handleCallBack.loadImage(imagesFromRes[position]);
}
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putInt(SELECTED_POSITION, position);
super.onSaveInstanceState(outState);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
this.position = position;
handleCallBack.loadImage(imagesFromRes[position]);
}
}
下面是ProfileDetailedFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ProfileDetailFragment extends Fragment {
private ImageView imageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_profile_detail, container, false);
imageView = (ImageView) viewGroup.findViewById(R.id.mainimage);
return viewGroup;
}
public void setImageView(int imgresid) {
imageView.setImageResource(imgresid);
}
}
我已经为“个人资料详细信息”创建了一个活动。请参见下面的代码。
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import TabBarActivity;
public class ProfileDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_detail);
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
finish();
return;
}
ProfileDetailFragment detailFragment = (ProfileDetailFragment) getSupportFragmentManager().findFragmentById(R.id.profile_detail_fragment);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("Back");
Bundle bundle = getIntent().getExtras();
if(bundle != null){
detailFragment.setImageView(bundle.getInt(TabBarActivity.IMAGE_ID));
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
finish();
}
return super.onOptionsItemSelected(item);
}
}
一个用于处理回调的接口。 HandleCallBack.java
public interface HandleCallBack {
public void loadImage(int imageres);
}
activity_profile_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/profile_detail_fragment"
android:name=".ProfileDetailFragment"/>
</LinearLayout>
“个人资料肖像”视图的布局。 fragment_master_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/profile_list_fragment"
android:name=".ProfileFragment"/>
</LinearLayout>
“轮廓横向”视图的布局。 fragment_master_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/profile_list_fragment"
android:name=".ProfileFragment"/>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:id="@+id/profile_detail_fragment"
android:name=".ProfileDetailFragment"/>
</LinearLayout>
fragment_profile_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/imagelist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@color/colorAccent"/>
</LinearLayout>
fragment_profile_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Detail View"
android:padding="10dp"
android:background="@android:color/black"
android:textColor="@android:color/white"
android:gravity="center"
android:layout_alignParentTop="true"
android:id="@+id/title"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainimage"
android:contentDescription="@string/app_name"
android:layout_below="@id/title"/>
我为纵向和横向视图创建了单独的布局。该应用在“人像”视图中正常运行。当我将手机转到横向视图并单击该项目时,其回调会引发错误。当我调试代码时,我发现TabBarActivity.java中的loadImage函数引起了问题。
@Override
public void loadImage(int imagesrc) {
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
ProfileDetailFragment detailFragment = (ProfileDetailFragment) getSupportFragmentManager().findFragmentById(R.id.profile_detail_fragment);
detailFragment.setImageView(imagesrc);
} else{
Intent intent = new Intent(this.getBaseContext(), ProfileDetailActivity.class);
intent.putExtra(IMAGE_ID, imagesrc);
startActivity(intent);
}
}
ProfileDetailFragment detailFragment =(ProfileDetailFragment)getSupportFragmentManager()。findFragmentById(R.id.profile_detail_fragment);正在重用null。无法初始化片段。
如何将这个主从视图放入viewpager中。首先将其加载到viewpager容器中。但是,在纵向视图中,当我单击某个项目时,它将在viewpager容器外部打开详细视图。我想将其粘贴在页面容器中。请帮助我是android编程的新手。如果您在viewpager内还有其他任何主/详细视图的工作示例,请分享。任何帮助都感激不尽。