gpt4 book ai didi

android - 减少 ViewPager 中的空垂直高度

转载 作者:行者123 更新时间:2023-11-29 01:04:36 25 4
gpt4 key购买 nike

这是我的 fragment 。上半部分的蓝色图像是可滑动的(使用 ViewPager 实现)。在它下面是一个圆形指示器。红色标记显示的垂直间隙对我来说似乎太多了。如何尽可能减少?

乍一看,我认为答案可能在vp_item.xml 中的android:padding 中。事实证明它没有达到我想要的。

enter image description here

附上一些相关代码。

NewsFragment.java

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationAdapter;

import net.devbyzero.app.pascatrisakti.NewsListAdapter;
import net.devbyzero.app.pascatrisakti.R;
import net.devbyzero.app.pascatrisakti.ViewPagerAdapter;
import net.devbyzero.app.pascatrisakti.WrapContentViewPager;
import net.devbyzero.app.pascatrisakti.data.NewsProvider;

import java.util.ArrayList;
import me.relex.circleindicator.CircleIndicator;


public class NewsFragment extends Fragment {

private RecyclerView recView;
private NewsListAdapter adapter;
private WrapContentViewPager vPager;
private PagerAdapter pAdapter;
private AHBottomNavigation bottomNavigation;
private AHBottomNavigationAdapter bottomAdapter;
private CircleIndicator llDots;

public static NewsFragment newInstance(String param1, String param2){
NewsFragment fragment = new NewsFragment();
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_news, container, false);
recView = (RecyclerView) view.findViewById(R.id.news_list);
recView.setLayoutManager(new LinearLayoutManager(getActivity()));

vPager = (WrapContentViewPager) view.findViewById(R.id.view_pager);
llDots = (CircleIndicator) view.findViewById(R.id.lldots);
int[] banners = new int[]{R.drawable.spmb, R.drawable.spmb, R.drawable.spmb};
pAdapter = new ViewPagerAdapter(getActivity(), banners);
vPager.setAdapter(pAdapter);
llDots.setViewPager(vPager);

bottomNavigation = (AHBottomNavigation) view.findViewById(R.id.bottom_navigation);
bottomNavigation.setTitleState(AHBottomNavigation.TitleState.SHOW_WHEN_ACTIVE);
bottomAdapter = new AHBottomNavigationAdapter(getActivity(), R.menu.bottom_menu);
int[] tabColors = getResources().getIntArray(R.array.tab_colors);
bottomAdapter.setupWithBottomNavigation(bottomNavigation, tabColors);
final String[] msgs = new String[]{"Berita", "Profil", "KRS"};

bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
Toast.makeText(getActivity(), "Pilihan: "+msgs[position], Toast.LENGTH_SHORT).show();
return true;
}
});

((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("News");

adapter = new NewsListAdapter(new NewsProvider().getNews());
recView.setAdapter(adapter);
return view;
}
}

WrapContentViewPager.java

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

public class WrapContentViewPager extends ViewPager {
public WrapContentViewPager(Context context) {
super(context);
}

public WrapContentViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height) height = h;
}

heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

ViewPagerAdapter.java

import android.content.Context;
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.ImageView;
import android.widget.RelativeLayout;

import net.devbyzero.app.pascatrisakti.R;

public class ViewPagerAdapter extends PagerAdapter {

Context context;
int[] flag;
LayoutInflater inflater;

public ViewPagerAdapter(Context context, int[] flag){
this.context = context;
this.flag = flag;
}

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

@Override
public boolean isViewFromObject(View view, Object object) {
return (view == (RelativeLayout) object);
}

@Override
public float getPageWidth(int position) {
return 0.7f;
}


@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgflag;

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.vp_item, container,false);

// Locate the ImageView in vp_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.flag);

// Capture position and set to the ImageView
imgflag.setImageResource(flag[position]);

// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);

return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}

vp_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="0.5dp" >

<ImageView
android:id="@+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:padding="0.25dp" />

</RelativeLayout>

fragment_news.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>

<!-- for displaying ads -->
<net.devbyzero.app.pascatrisakti.WrapContentViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</net.devbyzero.app.pascatrisakti.WrapContentViewPager>

<me.relex.circleindicator.CircleIndicator
android:id="@+id/lldots"
android:layout_width="match_parent"
android:layout_height="48dp"/>


<!-- for displaying news -->
<android.support.v7.widget.RecyclerView
android:id="@+id/news_list"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>

<!-- bottom navigation -->
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

最佳答案

这不是差距。这是您的 CircleIndicator View ,其高度为 48dp。

<me.relex.circleindicator.CircleIndicator
android:id="@+id/lldots"
android:layout_width="match_parent"
android:layout_height="48dp"/>

相应地修改它。最好使用框架或相对布局在具有透明背景的 WrapContentViewPager 上显示这些点。无论如何,这些点在该背景下几乎不可见。

关于android - 减少 ViewPager 中的空垂直高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48261766/

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