2017-01-25 1 views
-1

Я пытаюсь добавить данные API-интерфейса Parse с вкладкой Fragment. ListView работает нормально, и мне нужно добавить 2 кнопки фильтра между вкладками «Фрагмент» и «ListView». Ниже я прикрепил, что я пытается получитьParse-Server Android: добавление кнопок внутри вкладки TabFragment с помощью ListView

BUTTON SCREENSHOT

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; 
     } 
    } 
} 

Это вкладка фрагмент.
Я хочу 2 Пуговицы между вкладкой Фрагмент кнопок и элементов ListView

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()); 
      } 
     } 
    } 

это XML-файл в ListView

<?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> 

ответ

3

Если мы quote Android | ListFragment

ListFragment имеет макет по умолчанию, который состоит из одного вида списка. Однако, если вы желаете, вы можете настроить макет фрагмента, вернув свою собственную иерархию представлений из onCreateView (LayoutInflater, ViewGroup, Bundle).

А также из документации

иерархии вид необходимо содержать объект ListView с идентификатором "@android: идентификатор/список"

Давайте назовем это **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() уже вернет вам ваш ListView, поэтому обратите внимание, мне не нужно, чтобы найти его.Но если вы это сделали, вот как.

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

Итак, это просто вопрос создания файла XML с этим упомянутым значением идентификатора, установленным в ListView, как показано выше.

Смежные вопросы