我想在其他片段下面显示片段,但它是重叠的

时间:2016-04-14 06:38:37

标签: java android android-fragments android-asynctask

我试图在另一个具有imageview和两个文本视图的片段下面显示一个片段。 我正在使用异步任务从服务器下载图像及其标题和描述。 在异步任务中,我正在创建一个片段。

private class ContendDownloader extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... params) {

            final CloudQuery query = new CloudQuery("CollegeFeeds");
            query.orderByDesc("updatedAt");
            query.include("imageTitle");
            query.include("image");

            try {

                query.find(new CloudObjectArrayCallback() {
                    @Override
                    public void done(CloudObject[] images , CloudException e) throws CloudException {
                        for (int i = 0; i < images.length; i++){

                            Title =  images[i].get("imageTitle").toString();
                            Description = images[i].get("imageDescription").toString();

                            System.out.println(Title + Description);
                            final CloudFile f = new CloudFile(images[i].getDocument().getJSONObject("image"));
                            urlStr= f.getFileUrl();
                            Log.i("url-org:", f.getFileUrl());
                            urlStr=urlStr.replaceAll("beta-", "");
                            Log.i("url-modf:", urlStr);
                            try {
                                URL url = new URL(urlStr);
                                HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
                                connection.connect();

                                InputStream inputStream = new BufferedInputStream(url.openStream(),8192);

                                bitmap = BitmapFactory.decodeStream(inputStream);

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {

                                        bundle = new Bundle();
                                        bundle.putString("titleText", Title);
                                        bundle.putString("descriptionText", Description);
                                        bundle.putParcelable("bitmap", bitmap);


                                        FragmentManager fm = getSupportFragmentManager();
                                        FragmentTransaction ft = fm.beginTransaction();
                                        CollegeFeedFrag collegeFeedFrag = new CollegeFeedFrag();
                                        collegeFeedFrag.setArguments(bundle);
                                        ft.add(R.id.college_container1, collegeFeedFrag);
                                        ft.addToBackStack(null);
                                        ft.commit();

                                        numOfFragments++;
                                        Log.i("number of Fragments", Integer.toString(numOfFragments));





                                    }
                                });


                            } catch (MalformedURLException e1) {
                                e1.printStackTrace();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                });
            } catch (CloudException e) {
                e.printStackTrace();
            }





            return null;
        }

这是我的Fragment.java

package com.rakesh_kr.finaltoolbar;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class CollegeFeedFrag extends Fragment {

    ImageView imageView;
    TextView titleTextView, descriptionTextView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.college_feed_fragment, container, false);


        imageView = (ImageView) rootView.findViewById(R.id.imageView);
        titleTextView = (TextView) rootView.findViewById(R.id.titleTextView);
        descriptionTextView = (TextView) rootView.findViewById(R.id.descriptionTextView);

        imageView.setImageBitmap((Bitmap) getArguments().getParcelable("bitmap"));
        titleTextView.setText(getArguments().getString("titleText"));
        descriptionTextView.setText(getArguments().getString("descriptionText"));

        return rootView;


    }


}

通过使用上面的代码,我可以下载内容并以片段形式显示。 但问题是在Asynctask中创建的多个活动是重叠的。 请任何人帮忙。我的选择已经用完了。

我的基本想法是一次向用户显示所有内容。 例如:用户通过在屏幕上向上滑动看到第一张图像,他应该拍摄第二张图像。 这可能吗?

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.rakesh_kr.finaltoolbar.CollegeFeed">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_college_feed" />
    <!--<include layout="@layout/activity_main" />-->

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/fab_icon" />

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

fragment.xml之

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/college_container">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_marginBottom="200dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/titleTextView"
        android:layout_marginRight="210dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/descriptionTextView"
        android:layout_marginRight="150dp" />
</LinearLayout>

0 个答案:

没有答案