如何更新骨架布局代码,以使其不会阻塞viewpager?

时间:2019-05-06 20:47:20

标签: android android-layout android-viewpager

我正在尝试向我的ViewPager添加骨架布局/效果,但是由于某些问题,微光的布局会在我看到白屏而不是ViewPager数据内容后立即加载。

我当前正在使用这些库:

INSERT INTO x_post
            (`post_title`,
             `post_content`,
             `date_created`,
             `user_id`) 
            SELECT `post_title`,
                   `post_content`,
                   `date_created`,
                   `user_id`
                   FROM (SELECT $postTitle1 `post_title`,
                                $content1 `post_content`,
                                $time `date_created`,
                                $userId `user_id`
                         UNION ALL
                         ...
                         UNION ALL
                         SELECT $postTitle30 `post_title`,
                                $content30 `post_content`,
                                $time `date_created`,
                                $userId `user_id`) tmp
                   WHERE NOT EXISTS (SELECT *
                                            FROM x_news
                                            WHERE x_news.news_title = tmp.`post_title`);

这是我的代码:

 implementation 'com.ethanhua:skeleton:1.1.2'
 implementation 'io.supercharge:shimmerlayout:2.1.0'

上面的代码是我的骨架视图布局。

下面的代码是我在活动中的ViewPager代码:

<android.support.constraint.ConstraintLayout 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:background="@color/white"
    android:paddingTop="64dp">
    <View
        android:id="@+id/roomsTitle"
        style="@style/Headline2LeftBlack"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_gravity="center_vertical"
        android:background="@color/black_12"
        android:text="@string/meeting_rooms"
        android:textAppearance="@style/TextAppearance.Text.Chronicle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:paddingBottom="16dp"/>

    <View
        android:id="@+id/room1"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:clipToPadding="false"
        android:overScrollMode="never"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="16dp"
        android:paddingBottom="16dp"
        android:background="@color/black_12"
        app:layout_constraintTop_toBottomOf="@id/roomsTitle"  />


    <View
        android:id="@+id/room2"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:clipToPadding="false"
        android:overScrollMode="never"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="16dp"
        android:background="@color/black_12"
        app:layout_constraintTop_toBottomOf="@id/room1"  />


</android.support.constraint.ConstraintLayout>

由于某种原因

public class RoomListsActivity extends BaseActivity implements RoomListsMvpView {

    @Inject
    RoomListsPagerAdapter mPagerAdapter;


    @BindView(R.id.room_lists_view_pager)
    ViewPager mViewPager;

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;

    private SkeletonScreen skeletonScreen;

    public static Intent getStartIntent(Context context) {
        return new Intent(context, RoomListsActivity.class);
    }

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

        getActivityComponent().inject(this);

        setUnBinder(ButterKnife.bind(this));

        mPresenter.onAttach(this);

        setUp();
    }

    @Override
    protected void setUp() {
        setSupportActionBar(mToolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_backarrow);
            getSupportActionBar().setElevation(0);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }

        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
                TextView text = (TextView) tab.getCustomView();
                if (text != null) {
                    text.setTypeface(null, Typeface.BOLD);
                    text.setTextColor(ContextCompat.getColor(RoomListsActivity.this, R.color.reddish));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView text = (TextView) tab.getCustomView();
                if (text != null) {
                    text.setTypeface(null, Typeface.NORMAL);
                    text.setTextColor(ContextCompat.getColor(RoomListsActivity.this, R.color.brownish_grey));
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        });

        mRefresh.setOnRefreshListener(this::refreshassistant);

        mPresenter.getAvailableRooms();
    }


    @Override
    protected void onDestroy() {
        mPresenter.onDetach();
        super.onDestroy();
    }

    @Override
    public void showRooms(List<RoomList> roomLists) {
        int selectedTab = mTabLayout.getSelectedTabPosition();

        mPagerAdapter.setRooms(roomLists);

        if (mTabLayout.getTabCount() != roomLists.size()) {
            mTabLayout.removeAllTabs();
            for (int i = 0; i < roomLists.size(); i++) {
                mTabLayout.addTab(mTabLayout.newTab().setText(roomLists.get(i).getRoomTitle()));
            }

            mViewPager.setOffscreenPageLimit(mTabLayout.getTabCount());

            for (int i = 0; i < mTabLayout.getTabCount(); i++) {
                TabLayout.Tab tab = mTabLayout.getTabAt(i);
                if (tab != null) {
                    TextView tabTextView = new TextView(this);
                    tab.setCustomView(tabTextView);
                    tabTextView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
                    tabTextView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;

                    tabTextView.setText(tab.getText());

                    if (i == 0) {
                        tabTextView.setTypeface(null, Typeface.BOLD);
                        tabTextView.setTextColor(ContextCompat.getColor(this, R.color.reddish));
                    }
                }
            }

            if (selectedTab > 0 && selectedTab < mTabLayout.getTabCount()) {
                TabLayout.Tab tab = mTabLayout.getTabAt(selectedTab);
                if (tab != null) {
                    tab.select();
                }
            }
        }
    }

    @Override
    public void showLoading() {
        skeletonScreen = Skeleton.bind(mViewPager)
                .load(R.layout.item_skeleton_roomlist)
                .duration(1000)
                .color(R.color.shimmer_color)
                .angle(20)
                .shimmer(false)
                .show();

    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        skeletonScreen.hide();
        mRefresh.setRefreshing(false);
    }
}

覆盖了我的ViewPager代码,并显示了空白的空白屏幕而不是内容。知道我该如何解决这个问题吗?

在基类中,我正在调用:

{
skeletonScreen = Skeleton.bind(mViewPager)
                        .load(R.layout.item_skeleton_roomlist)
                        .duration(1000)
                        .color(R.color.shimmer_color)
                        .angle(20)
                        .shimmer(false)
                        .show();
}

1 个答案:

答案 0 :(得分:3)

我自己解决了这个问题。只需将viewpager添加到framelayout中,由于某种原因,该方法就可以起作用了。我添加的代码如下:

interface C {
    readonly c: string
}

interface TestInt {
    readonly a: string
    readonly b: number
    readonly c: C
}

const test: TestInt = {
    a: 'a',
    b: 1,
    c: {
        c: 'c',
    },
}

const v = myValue('c', test) // const v: C