android scrollview无法在片段中工作

aurhwmvo  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(429)

我正在做一个项目,其中继续与碎片导航抽屉,在我的主页碎片页上,我添加了这个布局。为了向viewpager、linear layout和gridview添加额外的数据,我使用java代码。我的问题是,scrollview没有进入我的home\u片段(只有gridiview没有滚动完整的片段活动)。我尝试过使用嵌套的scrollview,但它也不起作用。我提供了如下xml和java代码,
片段\u home.xml

<ScrollView
    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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:scrollbarSize="0dp"
    android:fillViewport="true"
    >

<RelativeLayout
        android:id="@+id/HomeFragmentRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPage"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_margin="5dp"
            />
        <LinearLayout
            android:id="@+id/SliderDots"
            android:layout_below="@+id/viewPage"
            android:orientation="horizontal"
            android:gravity="center_vertical|center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"/>

        <GridView
            android:id="@+id/simpleGridView"
            android:layout_below="@+id/SliderDots"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:gravity="center"
            android:numColumns="2"
            android:layout_margin="5dp"
            android:verticalSpacing="15dp"
            android:horizontalSpacing="15dp"
            />
    </RelativeLayout>
</ScrollView>

主页片段.java

public class HomeFragment extends Fragment {
    LinearLayout sliderDotspanel;
    private int dotscount;
    private ImageView[] dots;
    GridView simpleList;

    ArrayList<Item> birdList=new ArrayList<Item>();

    @SuppressLint("ClickableViewAccessibility")
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);

        ViewPager mViewPager = root.findViewById(R.id.viewPage);
        sliderDotspanel = root.findViewById(R.id.SliderDots);
        ImageAdapter adapterView = new ImageAdapter(getContext());
        mViewPager.setAdapter(adapterView);

        dotscount = adapterView.getCount();
        dots = new ImageView[dotscount];
        for(int i = 0; i < dotscount; i++){
            dots[i] = new ImageView(getContext());
            dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot_white));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(8, 0, 8, 0);
            sliderDotspanel.addView(dots[i], params);
        }
        dots[0].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot_black));

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                for(int i = 0; i< dotscount; i++){
                    dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot_white));
                }

                dots[position].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot_black));

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        simpleList = root.findViewById(R.id.simpleGridView);
        birdList.add(new Item("Amazone",R.drawable.rsz_amazon));
        birdList.add(new Item("Flipcart",R.drawable.rsz_flipkart));
        birdList.add(new Item("Myntra",R.drawable.rsz_myntra));
        birdList.add(new Item("Beauty",R.drawable.rsz_beauty));
        birdList.add(new Item("Health",R.drawable.rsz_pngwave));
        birdList.add(new Item("Entertainment",R.drawable.rsz_media));
        birdList.add(new Item("Stories",R.drawable.rsz_stories));
        birdList.add(new Item("Poems",R.drawable.rsz_poetry));
        birdList.add(new Item("Gadgets",R.drawable.rsz_gadgets));
        birdList.add(new Item("Videos",R.drawable.rsz_you_tube_new));

        MyAdapter myAdapter=new MyAdapter(getContext(),R.layout.grid_view_items,birdList);
        simpleList.setAdapter(myAdapter);

        simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(getContext(),Integer.toString(position) +" "+ Long.toString(id),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), SecondActivity.class);;
                switch (position){
                    case 0:
                        intent.putExtra("grid_position","0");
                        startActivity(intent);
                        break;
                    case 1:
                        intent.putExtra("grid_position","1");
                        startActivity(intent);
                        break;
                    case 2:
                        intent.putExtra("grid_position","2");
                        startActivity(intent);
                        break;
                    case 3:
                        intent.putExtra("grid_position","3");
                        startActivity(intent);
                        break;
                    case 4:
                        intent.putExtra("grid_position","4");
                        startActivity(intent);
                        break;
                    case 5:
                        intent.putExtra("grid_position","5");
                        startActivity(intent);
                        break;
                    case 6:
                        intent.putExtra("grid_position","6");
                        startActivity(intent);
                        break;
                    case 7:
                        intent.putExtra("grid_position","7");
                        startActivity(intent);
                        break;
                    case 8:
                        intent.putExtra("grid_position","8");
                        startActivity(intent);
                        break;
                    case 9:
                        intent.putExtra("grid_position","9");
                        startActivity(intent);
                        break;
                    default:
                        break;
                }
            }
        });

        return root;
    }
}

如果你想查看我的完整代码,请查看我的完整代码链接-https://drive.google.com/drive/folders/13settzasdnbukcyrufl3svu5eewa5wiz?usp=sharing
提前谢谢。

bnl4lu3b

bnl4lu3b1#

你不能直接应用滚动查看我希望这将工作

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ripple="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rel_homee"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true"
tools:context=".Fragment.HomeFragment">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarSize="0dp">

<RelativeLayout
    android:id="@+id/HomeFragmentRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPage"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="5dp"
        />
    <LinearLayout
        android:id="@+id/SliderDots"
        android:layout_below="@+id/viewPage"
        android:orientation="horizontal"
        android:gravity="center_vertical|center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"/>

    <GridView
        android:id="@+id/simpleGridView"
        android:layout_below="@+id/SliderDots"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:gravity="center"
        android:numColumns="2"
        android:layout_margin="5dp"
        android:verticalSpacing="15dp"
        android:horizontalSpacing="15dp"
        />
</RelativeLayout>
</ScrollView>
</RelativeLayout>

相关问题