Parse-Server Android:使用 ListView 在 Tab ListFragment 中添加按钮

Parse-Server Android : Adding buttons inside a Tab ListFragment with ListView

I'm trying to add Parse API data with a tab Fragment. The ListView is working fine and I need to add 2 filter Buttons between the Fragment tab Buttons and the ListView. Below I attached what I'm trying to get

MainActivity.java

import android.support.design.widget.TabLayout;
import android.support.v4.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.parse.Parse;

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

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


        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);


        Parse.initialize(new Parse.Configuration.Builder(this)
                .applicationId("")
                .server("")

                .build()
        );


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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);
    }



    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        private EventFragment eventFragment = new EventFragment();
        private IndividualsFragment individualsFragment = new IndividualsFragment();
        private OrganizationFragment orgFragment = new OrganizationFragment();



        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {



            switch (position) {
                case 0:

                    OrganizationFragment tab1= orgFragment;
                    return tab1;
                case 1:
                    EventFragment tab2= eventFragment;
                    return tab2;
                case 2:
                    IndividualsFragment tab3=  individualsFragment;
                    return tab3;
            }
            return null ;



        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Directory";
                case 1:
                    return "EVENTS";
                case 2:
                    return "INDIVIDUALS";
            }
            return null;
        }
    }
}

这是一个片段选项卡。
我想在片段选项卡按钮和 ListView 项目之间添加 2 个按钮

EventFragment.java

  import android.app.ListActivity;
    import android.app.ListFragment;
    import android.database.DataSetObserver;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.view.PagerAdapter;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import com.parse.FindCallback;
    import com.parse.ParseException;
    import com.parse.ParseObject;
    import com.parse.ParseQuery;

    import java.util.ArrayList;
    import java.util.List;


    public class  EventFragment
            extends android.support.v4.app.ListFragment
            implements FindCallback<ParseObject> {
        private List<ParseObject> mOrganization = new ArrayList<ParseObject>();


        @Override
        public void onViewCreated(View view, Bundle b) {
            super.onViewCreated(view, b);

            EventAdapter adaptor = new EventAdapter(getActivity(), mOrganization);
            setListAdapter(adaptor);

            // This is like calling fetchList()
            ParseQuery.getQuery("Event").findInBackground(this);
        }

        /**
         * This is needed by implementing the callback on the class
         **/
        @Override
        public void done(List<ParseObject> scoreList, ParseException e) {
            if (e == null) {
                Log.d("score", "Retrieved " + scoreList.size() + " Event");
                mOrganization.clear();
                mOrganization.addAll(scoreList);

                ((EventAdapter) getListAdapter()).notifyDataSetChanged();
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    }

这是 ListView

的 xml 文件
<?xml version="1.0" encoding="utf-8"?>



<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.swipe.com.MainActivity">


    <com.example.rihan.swip.RoundRectCornerImageView
        android:id="@+id/imageView"


        android:layout_gravity="center"
        android:layout_height="110dp"
        android:layout_width="110dp"
        tools:minWidth="30dp"
        tools:maxWidth="50dp"

        tools:minHeight="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:layout_marginTop="19dp"

        android:scaleType="fitXY"/>


    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/organizationname"
        tools:textSize="10sp"
        android:textSize="10sp"
        android:layout_below="@+id/idposition"
        android:layout_alignLeft="@+id/idposition"
        android:layout_alignStart="@+id/idposition"
        android:paddingTop="10px" />

    <TextView
        android:id="@+id/user"
        android:text="Username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="12sp"
        android:textColor="#000"
        android:layout_alignBaseline="@+id/name"
        android:layout_alignBottom="@+id/name"
        android:layout_toRightOf="@+id/name"
        android:layout_toEndOf="@+id/name"
        android:paddingLeft="10dp"
        android:fontFamily="sans-serif" />

    <TextView
        android:text="yy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/idposition"

        android:textSize="10sp"
        android:layout_below="@+id/name"
        android:layout_alignLeft="@+id/name"
        android:layout_alignStart="@+id/name" />

    <TextView
        android:id="@+id/name"
        android:text="Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:minWidth="5dp"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        tools:textStyle="bold"
        android:textColor="#000"
        android:textSize="12sp"
        android:textStyle="bold"
        android:layout_alignTop="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView"
        android:layout_marginTop="13dp"
        android:fontFamily="sans-serif" />

</RelativeLayout>

这是activity_main.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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.swipe.com.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">



        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:layout_height="40dp"
                android:background="@drawable/nav"
                android:id="@+id/button2"
                android:layout_weight="1"

                android:layout_width="40dp"
                android:layout_alignBottom="@+id/button"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                tools:paddingLeft="25dp"
                tools:layout_marginLeft="20dp" />

            <Button
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/i"
                android:id="@+id/button"
                android:layout_weight="1"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

            <ImageView
                app:srcCompat="@drawable/logo"
                android:id="@+id/imageView2"
                android:layout_weight="1"
                tools:layout_marginLeft="15dp"
                android:layout_width="280dp"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/button"
                android:layout_toStartOf="@+id/button"
                android:layout_height="50dp" />

        </RelativeLayout>


        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


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

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

如果我们引用Android | ListFragment

ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle).

也来自文档

your view hierarchy must contain a ListView object with the id "@android:id/list"

我们称之为**some_list_view.xml**

注意不是@+id/list,而是@android:id/list

 <?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:paddingLeft="8dp"
         android:paddingRight="8dp">

  <!-- TODO: Add your buttons here -->

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_weight="1"/>

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:text="No data"/>
 </LinearLayout>

onViewCreated 中存在您的错误之一。你那里没有充气机。

按照文档中的说明实现另一种方法。

而且您不需要找到 ListView。
使用 findViewById 查找其他视图。
例如,

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.some_list_view, parent, false);
    }

    @Override
    public void onViewCreated(View view, Bundle b) {
        super.onViewCreated(view, b);

        // Button filter1 = (Button) view.findViewById(...);
        // filter1.setOnClickListener....

        EventAdapter adaptor = new EventAdapter(getActivity(), mOrganization);
        setListAdapter(adaptor);

        // This is like calling fetchList()
        ParseQuery.getQuery("Event").findInBackground(this);
    }

    @Override
    public void done(List<ParseObject> scoreList, ParseException e) {
        if (e == null) {
            ...
        }
    }

getListView() 已经 return 你的 ListView,因此请注意,我不需要找到它。但如果你这样做了,就是这样。

(ListView) view.findViewById(android.R.id.list);

所以,这只是制作一个 XML 文件并将上述 ID 值设置为 ListView 的问题,如上所示。