必须向上滚动才能看到上方布局项

时间:2017-06-01 19:04:37

标签: android xml layout android-recyclerview

当活动开始时,你看到的第一件事就是recylerview而不是更高的项目!

第一个元素是cardview,包括imageview,然后是包含radiobutton组的cardview。要访问这些项目,您必须向上滚动屏幕!

没有错误,我该如何解决这个问题?

注意:我的问题解决了当我不使用nestedScrollView但在这种情况下,最终的cardview(icluding imageview)没有出现

这是xml代码

  <FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginBottom="3dp"
        android:layout_marginTop="3dp">

        <ImageView
            android:id="@+id/sarasari_mainact_iv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:adjustViewBounds="true"
            android:src="@drawable/ad12_20" />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="3dp"
        android:layout_marginLeft="3dp">

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_horizontal"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="5dp"
            android:id="@+id/reshteha_radio_gruop">

            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="math" />
            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="science" />
            <RadioButton
                android:id="@+id/radioButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="human scinces" />
            <RadioButton
                android:id="@+id/radioButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="language" />
            <RadioButton
                android:id="@+id/radioButton5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="religion" />

        </RadioGroup>

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginBottom="6dp"
        android:layout_marginTop="3dp">

        <ImageView
            android:id="@+id/sarasari_mainact_iv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:adjustViewBounds="true"
            android:src="@drawable/ad12_20" />

    </android.support.v7.widget.CardView>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>

<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"/>

这是活动代码

    public class Main2Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public RadioButton rb1,rb2,rb3,rb4,rb5;
public Typeface vazirTf ;
private RadioGroup reshtehaRg ;
String[] mArrayNames = {"a","b","c","d","e","f","g","h","i"};
int[] mArrayPics = { R.drawable.pic1 , R.drawable.pic2 , R.drawable.pic3 , R.drawable.pic4, R.drawable.pic5 , R.drawable.pic6, R.drawable.pic7,R.drawable.pic8,R.drawable.pic9 };
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener= new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                mTextMessage.setText(R.string.title_home);
                return true;
            case R.id.navigation_dashboard:
                mTextMessage.setText(R.string.title_dashboard);
                return true;
            case R.id.navigation_notifications:
                mTextMessage.setText(R.string.title_notifications);
                return true;
            case R.id.navigation_about:
                mTextMessage.setText(R.string.title_news);
                return true;
        }
        return false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    itemsSetter();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    mRecyclerView= (RecyclerView)findViewById(R.id.my_recycler_view);
    mLayoutManager= new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MyAdapter(mArrayNames,mArrayPics,Main2Activity.this);
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.setNestedScrollingEnabled(false);// this item makes the recylcerview scrolling softly inside using nestedScrollView and commenting this code has no action to my problem (showing recylcerview istead uper elements)
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}


@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.nav_camera) {
    } else if (id == R.id.nav_gallery) {
    } else if (id == R.id.nav_slideshow) {
    } else if (id == R.id.nav_manage) {
    } else if (id == R.id.nav_share) {
    } else if (id == R.id.nav_send) {
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

public void itemsSetter() {
    vazirTf = Typeface.createFromAsset(getAssets(), "font/Vazir.ttf");
    rb1 = (RadioButton) findViewById(R.id.radioButton1);
    rb2 = (RadioButton) findViewById(R.id.radioButton2);
    rb3 = (RadioButton) findViewById(R.id.radioButton3);
    rb4 = (RadioButton) findViewById(R.id.radioButton4);
    rb5 = (RadioButton) findViewById(R.id.radioButton5);
    rb1.setTypeface(vazirTf);
    rb2.setTypeface(vazirTf);
    rb3.setTypeface(vazirTf);
    rb4.setTypeface(vazirTf);
    rb5.setTypeface(vazirTf);
    reshtehaRg = (RadioGroup)findViewById(R.id.reshteha_radio_gruop);
 }
}

2 个答案:

答案 0 :(得分:3)

您需要在descendantFocusability下的第一个布局中添加android.support.v4.widget.NestedScrollView,如:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants" >

答案 1 :(得分:1)

我想这是你问题的解决方案。检查此链接:

RecycleView steals focus when inside a NestedScrollView