gpt4 book ai didi

java - 有人可以向我展示 PagerSlidingTabStrip 的简单工作实现吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:21 29 4
gpt4 key购买 nike

这是图书馆:https://github.com/astuetz/PagerSlidingTabStrip我正在尝试实现它,但我的应用程序在启动时一直崩溃。我试图理解发布的示例应用程序,但认为我做的不对。这是我的代码:

主 Activity .java

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.astuetz.PagerSlidingTabStrip;


public class MainActivity extends FragmentActivity {

private final Handler handler = new Handler();

private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;

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

tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());

pager.setAdapter(adapter);

tabs.setViewPager(pager);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
}

private Drawable.Callback drawableCallback = new Drawable.Callback() {
@Override
public void invalidateDrawable(Drawable who) {
getActionBar().setBackgroundDrawable(who);
}

@Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
handler.postAtTime(what, when);
}

@Override
public void unscheduleDrawable(Drawable who, Runnable what) {
handler.removeCallbacks(what);
}
};

public class MyPagerAdapter extends FragmentPagerAdapter{
private final String[] TITLES = {"T1","T2"};

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

public CharSequence getPageTitle(int position){
return TITLES[position];
}
@Override
public Fragment getItem(int i) {
return null;
}

@Override
public int getCount() {
return TITLES.length;
}
}
}

这里是activity_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="edu.purdue.test.app.MainActivity">

<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tabs"
tools:context=".MainActivity" />


</RelativeLayout>

最佳答案

您没有在 FragmentPagerAdapter、getItem 方法中实例化任何您希望成为选项卡的 fragment 。相反,您将返回 null,因此没有 View 可以填充 viewpager。我可以看到您已经查看了他的示例项目,或许可以仔细查看您尝试添加到 viewpager 的 fragment 。上面的答案看起来是一个合理的实现。

关于java - 有人可以向我展示 PagerSlidingTabStrip 的简单工作实现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23234062/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com