首页 > 解决方案 > 默认视图卡在顶部,无论如何要删除它

问题描述

我在开发 android 应用程序时遇到问题,我在DrawerLayout嵌套中创建了一个默认视图,recyclerview但是当应用程序json从服务器获取而带有数据的视图显示在其下方时,该视图卡住了。

下面是我为 mainactivity.xml content.xml、adapter.java 类和 mainactivity.java 类包含的代码

主要活动.xml

    <?xml version="1.0" encoding="utf-8"?>
     <android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

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

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/customeView"
                        android:scrollbars="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

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

                </LinearLayout>

            </android.support.v4.widget.NestedScrollView>

        </android.support.v4.widget.SwipeRefreshLayout>


    </android.support.design.widget.CoordinatorLayout>

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />


</android.support.v4.widget.DrawerLayout>``

content.xml
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="11dp"
        android:layout_marginRight="11dp"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="7dp"
        app:cardCornerRadius="10dp">

        <RelativeLayout
            android:background="?android:attr/selectableItemBackground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/image"
                android:transitionName="img"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                tools:ignore="UnusedAttribute" />

            <ImageView
                android:id="@+id/shadow_bottom"
                android:src="@drawable/bottom_shadow"
                android:layout_alignBottom="@id/image"
                android:layout_width="match_parent"
                android:layout_height="80dp" />

            <FrameLayout
                android:id="@+id/layoutDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/image"
                android:background="@drawable/round_white"
                android:padding="5dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="20dp"
                android:layout_marginTop="-50dp">

                <ImageView
                    android:src="@drawable/ic_date"
                    android:layout_width="18dp"
                    android:layout_height="18dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp" />
                <TextView
                    android:textColor="#606060"
                    android:id="@+id/publishedAt"
                    android:layout_marginLeft="27dp"
                    android:layout_marginRight="10dp"
                    android:text="01 January 1990"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />


            </FrameLayout>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-light"
                android:textStyle="bold"
                android:textColor="@color/colorTextTitle"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:text="Title"
                android:textSize="17sp"
                android:layout_marginTop="10dp"
                android:layout_below="@id/image"
                android:id="@+id/title" />


            <TextView
                android:id="@+id/txtContents"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="5dp"
                android:text="Desc"
                />

            <TextView
                android:id="@+id/source"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_below="@id/txtContents"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="16dp"
                android:layout_marginBottom="10dp"
                android:fontFamily="sans-serif-light"
                android:textStyle="bold"
                android:textColor="@color/colorTextTitle"
                android:maxLines="1"
                android:drawablePadding="10dp"
                android:singleLine="true"
                android:ellipsize="end"
                android:text="Source" />

        </RelativeLayout>

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

   </FrameLayout>``

Adapter.java
    ``public class Adapter extends 
    RecyclerView.Adapter<Adapter.AdapterViewHolder> {

    private List<Articles> dataList;
    private Context context;
    private String storage_url="http://192.168.56.1/muslimnews/storage/";


    public Adapter(Context context,List<Articles> dataList){
        this.context = context;
        this.dataList = dataList;
    }

    class AdapterViewHolder extends RecyclerView.ViewHolder {

        public final View mView;

        TextView txtTitle;
        TextView txtContents;
        TextView source;
        TextView date;
        private ImageView coverImage;


        AdapterViewHolder(View itemView) {
            super(itemView);
            mView = itemView;

            txtTitle = mView.findViewById(R.id.title);
            txtContents=mView.findViewById(R.id.txtContents);
            coverImage = mView.findViewById(R.id.image);
            source=mView.findViewById(R.id.source);
            date=mView.findViewById(R.id.publishedAt);

        }
    }

    @NonNull
    @Override
    public AdapterViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
        View view=layoutInflater.inflate(R.layout.content_main,parent,false);

        return new AdapterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterViewHolder holder, int position) {
        holder.txtTitle.setText(dataList.get(position).getTitle());
        holder.txtContents.setText(Html.fromHtml(dataList.get(position).getShort_content()));
        holder.source.setText(dataList.get(position).getSource());


        try {
           Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dataList.get(position).getCreated_at());
           String publishedDate=new SimpleDateFormat("dd MMMM yyyy").format(date);


            holder.date.setText(publishedDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        Picasso.Builder builder = new Picasso.Builder(context);
        builder.downloader(new OkHttp3Downloader(context));


        builder.build().load(storage_url+dataList.get(position).getImgUrl())

                .placeholder((R.drawable.ic_launcher_background))
                .error(R.drawable.ic_launcher_background)
                .into(holder.coverImage);

    }

    @Override
    public int getItemCount() {

        return dataList==null ? 0: dataList.size();
    }


}``

mainactivity.java

    `public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, 
     SwipeRefreshLayout.OnRefreshListener {

    private Adapter adapter;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;

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


        GetDataService service= RetrofitClientInstance.getRetrofitInstance()
                .create(GetDataService.class);
        Call<List<Articles>> call=service.getAllArticles();

        call.enqueue(new Callback<List<Articles>>() {
            @Override
            public void onResponse(Call<List<Articles>> call, Response<List<Articles>> response) {


                recyclerView=findViewById(R.id.customeView);
                adapter=new Adapter(MainActivity.this,response.body());
                RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setNestedScrollingEnabled(false);
                recyclerView.setAdapter(adapter);

                //generateDataList(response.body());

                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailure(Call<List<Articles>> call, Throwable t) {
                Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_LONG).show();
            }
        });
        swipeRefresh();
        createMenu();





    }








    private void swipeRefresh(){
        swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
    }
    private void createMenu(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        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.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void fillMain(){
        GetDataService service= RetrofitClientInstance.getRetrofitInstance()
                .create(GetDataService.class);
        Call<List<Articles>> call=service.getAllArticles();

        call.enqueue(new Callback<List<Articles>>() {
            @Override
            public void onResponse(Call<List<Articles>> call, Response<List<Articles>> response) {
                swipeRefreshLayout.setRefreshing(false);

                generateDataList(response.body());
            }

            @Override
            public void onFailure(Call<List<Articles>> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(true);
                Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_LONG).show();
            }
        });
    }

    private  void generateDataList(List<Articles> articlesList){

        recyclerView=findViewById(R.id.customeView);
        adapter=new Adapter(this,articlesList);
        adapter.notifyDataSetChanged();
        RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
        recyclerView.setLayoutManager(layoutManager);
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);

    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } 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;
    }

    @Override
    public void onRefresh() {
       fillMain();
    }
}`

标签: android

解决方案


将您的清单主题设置为 Appcomat.theme.nocationbar 并将自定义工具栏或操作栏置于活动中。

   <application
    android:name="androidx.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:appComponentFactory="whateverString"
    android:debuggable="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:screenOrientation="portrait"
    android:supportsRtl="true"
    android:testOnly="false"
    android:theme="@style/AppTheme" //here you can edit it. completly.
    android:usesCleartextTraffic="true"
    tools:ignore="GoogleAppIndexingWarning,HardcodedDebugMode"
    tools:replace="android:appComponentFactory"
    tools:targetApi="m">

推荐阅读