如何使listview中的项目可滚动?

时间:2017-07-18 23:34:57

标签: android

所以我一直在查看多个StackOverflow线程,但我无法找到问题的解决方案。我有一个可扩展的列表视图,子元素包含一个叫做pdfview(https://github.com/barteksc/AndroidPdfViewer)的东西。单击父元素并显示PDF时,我收到一个文件。我的课程代码如下:

parentlistview.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">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

childlistview.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:weightSum="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfViewList"
            android:layout_width="352dp"
            android:layout_height="565dp"
            android:nestedScrollingEnabled="true"/>
    </LinearLayout>
</LinearLayout>

ParentListView.java

public class ParentListView {

    private TCSEvent tcsEvent;

    private ArrayList<ChildListView> children;

    public ArrayList<ChildListView> getChildren() {
        return children;
    }

    public void setChildren(ArrayList<ChildListView> children) {
        this.children = children;
    }

    public TCSEvent getTcsEvent() {

        return tcsEvent;
    }

    public void setTcsEvent(TCSEvent tcsEvent) {
        this.tcsEvent = tcsEvent;
    }
}

ChildListView.java

public class ChildListView {

    private String date;
    private String time;
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTime() {

        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getDate() {

        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

MyExpandableListAdapter.java

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.StorageReference;
import com.shockwave.pdfium.PdfDocument;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static android.R.attr.progress;
import static android.view.View.SCROLL_AXIS_VERTICAL;
import static com.tcs.tcseventscalendar.R.id.pdfView;

/**
 * Created by Aatika on 7/13/17.
 */

public class MyExpandableListAdapter extends BaseExpandableListAdapter implements OnPageChangeListener, OnLoadCompleteListener {
    private Context context;
    private ArrayList<TCSEvent> parents;
    private LayoutInflater inflater;
    private StorageReference mStorageReference;
    private int pageNumber = 0;
    private PDFView pdfView;
    private String localFileString;

    public MyExpandableListAdapter(Context context, ArrayList<TCSEvent> parents, StorageReference mStorageReference) {
        this.mStorageReference = mStorageReference;
        this.context = context;
        this.parents = parents;

        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getGroupCount() {
        return parents.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int size=0;
        if(parents.get(groupPosition).getFileName()!=null)
            return 1;
        else return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parents.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return parents.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView) {
        final TCSEvent parent = parents.get(groupPosition);

        // Inflate parentlistview.xml file for parent rows
        convertView = inflater.inflate(R.layout.parentlistview, parentView, false);

        ((TextView) convertView.findViewById(R.id.text1)).setText(parent.toString());

        return convertView;
    }

    // This Function used to inflate child rows view
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView) {
        final TCSEvent parent = parents.get(groupPosition);

        final String child = parent.getFileName();
        Toast.makeText(context, "CHILDVIEW", Toast.LENGTH_SHORT).show();
        convertView = inflater.inflate(R.layout.childlistview, parentView, false);
        pdfView = (PDFView) convertView.findViewById(R.id.pdfViewList);

        this.getFileLocation(parent, pdfView);


        return convertView;
    }

    private void displayFromString(String fileName, PDFView pdfView) {
        System.out.println("DISPLAY FROM STRING METHOD");
        System.out.println(fileName);
        pdfView.fromUri(Uri.parse(fileName))
                .defaultPage(pageNumber)
                .enableSwipe(true)

                .swipeHorizontal(false)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(context))
                .load();
        pdfView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                System.out.println("PDFVIEW");
//                getView
                return false;
            }
        });
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private void getFileLocation(final TCSEvent event, final PDFView pdfView) {
        String fileName = event.getFileName();
        System.out.println(fileName);
        StorageReference pdfRef = mStorageReference.child(fileName);

        final File localFile = new File(context.getExternalCacheDir(), event.getTitle()+event.getEpochDate() + ".pdf");
        localFileString = localFile.toString();

        if(!localFile.exists()) {
            System.out.println("FILE BEING CREATED");
            pdfRef.getFile(localFile)
                    .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            System.out.println("SUCESS METHOD");
                            event.setFileLocation(localFileString);
                            displayFromString(localFileString, pdfView);
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            e.printStackTrace();
                            Toast.makeText(context, "Something went wrong when fetching the file", Toast.LENGTH_SHORT).show();
                        }
                    });
        } else {
            System.out.println("ALREADY EXISTS");
            displayFromString(localFileString, pdfView);
        }

    }

    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
//        setTitle(String.format("%s %s / %s", localFileString, page + 1, pageCount));
    }

    @Override
    public void loadComplete(int nbPages) {
        printBookmarksTree(pdfView.getTableOfContents(), "-");

    }

    public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
        for (PdfDocument.Bookmark b: tree) {


            if (b.hasChildren()) {
                printBookmarksTree(b.getChildren(), sep + "-");
            }
        }
    }
}

我使用的列表视图是常规的可扩展列表视图。它在活动类中 ExpandableListView eventsListView = (ExpandableListView) findViewById(R.id.events_listview);

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你可以在ScrollViews中包装LinearLayouts,也可以这样做