私は Google からいくつかのコードを実装しています (SlidingTabLayout
とSlidingTabStrip
、オンラインで簡単に見つかります) でそれを使用してViewPager
、Fragment
. 問題は、メインのタブフラグメントクラスの拡張機能として使用PagerAdapter
していることです。これは、アダプタがタブの内容を表示するために使用されます。私のフラグメントCuteCollectionFragment.java
で機能しているタブの基本的なビューを取得できますが、それらをクリックすると、通常、新しいタブが強調表示されるまでに 2 回のクリックが必要です。さらに、スワイプして新しいタブに移動することはできません。私の疑いでは、通常はメイン アクティビティ用のベース コードをFragment
( my のために) に配置する必要があるため、既にスライドNavigation Drawer
している my と競合している可能性があります。Navigation Drawer
しかし、それは単なる推測です。
基本的に、Fragments
アクティビティではなく を使用する必要があります。しかし、それらはどのように競合しており、どうすればそれを修正できますか? 私が見つけたオンラインの例はすべて、アダプターの拡張機能としてではFragmentPagerAdapter
なく使用することを望んでいますが、 v.4 をサポートPagerAdapter
する を使用する必要があるため、それを行うことはできません。各ナビゲーション ドロワー メニュー項目をクリックして に移動できないため、FragmentManager
自分の(ナビゲーション ドロワー コードがあるもの) でそれを使用することはできません。その部分の switch ステートメントで赤いエラーが発生します。私のフラグメントはすべて拡張する必要がありますが、ナビゲーション ドロワーでは拡張できません。拡張する必要があります。CuteMainActivity
Fragment
Fragment fragment
FragmentActivity
Fragment
とにかく、これはとても紛らわしいです。主に2つのことを知りたいだけです:
タブがスライドしないのはなぜですか (なぜ 2 回クリックする必要があるのですか)
TabsAdapter
内部クラスを接続して、タブ クラス (すべて ) を表示するにはどうすればよいですかFragments
。タブ フラグメント クラス/xml の準備はすべて整っていますが (たとえば、PhotoTab.java、VideoTab.java など)、それらをタブ内のビューに接続するにはどうすればよいですか?
実行して自分で調べたい場合は、これが私のプロジェクト全体です。 アイコレクトキュートアプリ
Fragment
スライダータブコードを入れようとしているものと、それらが描画するxmlのみを以下に含めます。さらにファイルが必要な場合は、お知らせください。他に必要になる可能性がある (おそらく必要ない) ものは、呼び出された Google クラスとSlidingTabLayout
、すべての Navigation Drawer コードを配置したSlidingTabStrip
私の場所だけです。しかし、スライドタブを実装する必要があるため、CuteMainActivity
追加/エラーを探す主な場所は私のクラスです。CuteCollectionFragment
この SO postからコードを取得するのに役立ちました。
ご協力いただきありがとうございます!
CuteCollectionFragment.java
package org.azurespot.cutecollection;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.azurespot.R;
/**
* Created by mizu on 1/26/15.
*/
public class CuteCollectionFragment extends Fragment {
static final String LOG_TAG = "SlidingTabsBasicFragment";
private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;
FragmentManager fragmentManager;
View photoView;
View videoView;
View audioView;
View textView;
private static final int PHOTO_TAB = 0;
private static final int VIDEO_TAB = 1;
private static final int AUDIO_TAB = 2;
private static final int TEXT_TAB = 3;
public CuteCollectionFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_cute_collection,
container, false);
photoView = inflater.inflate(R.layout.photo_tab,
container, false);
videoView = inflater.inflate(R.layout.video_tab,
container, false);
audioView = inflater.inflate(R.layout.audio_tab,
container, false);
textView = inflater.inflate(R.layout.text_tab,
container, false);
// Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
fragmentManager = getActivity().getFragmentManager();
mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
mViewPager.setAdapter(new TabsAdapter());
mSlidingTabLayout = (SlidingTabLayout) rootView.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//
}
class TabsAdapter extends PagerAdapter {
/**
* @return the number of pages (tabs) to display
*/
@Override
public int getCount() {
return 5;
}
/**
* @return true if the value returned from
* {@link #instantiateItem(ViewGroup, int)} is the same object
* as the {@link View} added to the {@link ViewPager}.
*/
@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}
// BEGIN_INCLUDE (pageradapter_getpagetitle)
/**
* Return the title of the item at {@code position}. This is important
* as what this method returns is what is displayed in the
* {@link SlidingTabLayout}.
* <p>
* Here we construct one using the position value, but for real
* application the title should refer to the item's contents.
*/
@Override
public CharSequence getPageTitle(int position) {
return "Item " + (position + 1);
}
// END_INCLUDE (pageradapter_getpagetitle)
/**
* Instantiate the {@link View} which should be displayed at
* {@code position}. Here we inflate a layout from the apps resources
* and then change the text view to signify the position.
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Inflate a new layout from our resources
View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
container, false);
// Add the newly created View to the ViewPager
container.addView(view);
// Retrieve a TextView from the inflated View, and update it's text
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(String.valueOf(position + 1));
// Log.i(LOG_TAG, "instantiateItem() [position: " + position + "]");
// Return the View
return view;
}
/**
* Destroy the item from the {@link ViewPager}. In our case this is
* simply removing the {@link View}.
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
// Log.i(LOG_TAG, "destroyItem() [position: " + position + "]");
}
}
}
fragment_cute_collection.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"
android:background="#2198bb" >
<!--<android.support.v7.widget.Toolbar-->
<!--xmlns:app="http://schemas.android.com/apk/res-auto"-->
<!--android:id="@+id/toolbar"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:minHeight="?attr/actionBarSize"-->
<!--app:theme="@style/ThemeOverlay.AppCompat.ActionBar">-->
<org.azurespot.cutecollection.SlidingTabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--</android.support.v7.widget.Toolbar>-->
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:background="@android:color/white" />
</RelativeLayout>
pager_item.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"
android:gravity="center">
<TextView
android:id="@+id/item_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Page:"/>
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="80sp" />
</LinearLayout>
これが私のアダプタークラスの1つです。各タブには、独自のクラスとアダプター クラスがあります。これは 1 つです (ただし、方法がわからないため、まだ CuteCollectionFragment.java に接続していません)。
PhotoTab.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import org.azurespot.R;
/**
* Created by mizu on 2/8/15.
*/
public class PhotoTab extends Fragment{
private GridView gridView;
private GridViewPhotoAdapter gvPhotoAdapter;
public PhotoTab(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.photo_tab, container, false);
// with fragments, make sure you include the rootView when finding id
gridView = (GridView) v.findViewById(R.id.photo_grid);
// Create the Custom Adapter Object
gvPhotoAdapter = new GridViewPhotoAdapter(getActivity());
// Set the Adapter to GridView
gridView.setAdapter(gvPhotoAdapter);
return v;
}
}
PhotoTab.java、GridViewPhotoAdapter.javaのアダプター
package org.azurespot.cutecollection;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import org.azurespot.R;
/**
* Created by mizu on 2/5/15.
*/
public class GridViewPhotoAdapter extends ArrayAdapter {
public Context context;
public ImageView mediaPhoto;
public GridViewPhotoAdapter(Context context) {
super(context, 0);
this.context = context;
}
public int getCount() {
return 24;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.photo_grid_row, parent, false);
mediaPhoto = (ImageView) row.findViewById(R.id.photo_view);
mediaPhoto.setImageResource(R.drawable.ic_collection_add);
}
return row;
}
}