这是我的片段。上部的蓝色图像是可以跳过的(使用ViewPager实现)。下面是一个圆圈指示器。红色标记所显示的垂直间隙对我来说似乎太过分了。如何尽可能减少它?
乍一看,我认为答案可能在 vp_item.xml 中的 android:padding 。事实证明它没有达到我想要的目的。
附上一些相关代码。
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>
答案 0 :(得分:1)
这不是一个差距。它是CircleIndicator
视图,其高度为48dp。
<me.relex.circleindicator.CircleIndicator
android:id="@+id/lldots"
android:layout_width="match_parent"
android:layout_height="48dp"/>
相应地修改它。
最好使用框架或相对布局在WrapContentViewPager
上以透明背景显示此点。无论如何,这些点几乎都不可见。