Поместите карточку внутри фрагмента - PullRequest
1 голос
/ 21 сентября 2019

Я сделал меню вкладок, используя несколько фрагментов.И я собираюсь поместить карточку в каждый фрагмент.Я попробовал поискать в Google, но все, что мне нужно было сделать, это включить просмотр карты в активность.

Я бы хотел добавить просмотр карты во фрагмент, а затем я хотел бы реализовать инициацию заранее заданного набора действий, когдаЯ нажимаю на карточку.

Как мне это сделать?

<FrameLayout 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"
  tools:context="com.aeyoung.csw.CommunityFragment">

  <!-- TODO: Update blank fragment layout -->

  <android.support.constraint.ConstraintLayout 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"
      tools:context="com.aeyoung.csw.CommunityFragment">

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

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

  </android.support.constraint.ConstraintLayout>
</FrameLayout>
  • Также я добавляю код фрагмента (файл Java)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

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

public class CommunityFragment extends AppCompatActivity {
    //Create parameter
    List<Product> productList = new ArrayList<>();

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

        // initialize parameter "productList"
        setInitialData();

        //Find RecyclerView from fragment_community.xml
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);

        // Create LinearLayoutManager and set it to RecyclerView
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        //Create MyAdapter object with the parameters and set to RecyclerView
        MyAdapter myAdapter = new MyAdapter(this, productList);
        recyclerView.setAdapter(myAdapter);
    }

    private void setInitialData() {
        productList.add(new Product("text1", "text1", R.mipmap.ic_launcher));
        productList.add(new Product("text2", "text2", R.mipmap.ic_launcher));
        productList.add(new Product("text3", "text3", R.mipmap.ic_launcher));
        productList.add(new Product("text4", "text4", R.mipmap.ic_launcher));
        productList.add(new Product("text5", "text5", R.mipmap.ic_launcher));
        productList.add(new Product("text6", "text6", R.mipmap.ic_launcher));
        productList.add(new Product("text7", "text7", R.mipmap.ic_launcher));
        productList.add(new Product("text8", "text8", R.mipmap.ic_launcher));
        productList.add(new Product("text9", "text9", R.mipmap.ic_launcher));
        productList.add(new Product("text10", "text10", R.mipmap.ic_launcher));
    }
}

PageAdapter.java code

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Created by abdalla on 2/18/18.
 */

public class PageAdapter extends FragmentPagerAdapter {

    private int numOfTabs;

    PageAdapter(FragmentManager fm, int numOfTabs) {
        super(fm);
        this.numOfTabs = numOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new MajorFragment();
            case 1:
                return new FresherFragment();
            case 2:
                return new CommunityFragment();
            case 3:
                return new SettingsFragment();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return numOfTabs;
    }
}

И при сборке возникает следующая ошибка.«ошибка: несовместимые типы: CommunityFragment не может быть преобразован во фрагмент»

1 Ответ

2 голосов
/ 22 сентября 2019

как говорит ошибка "error: incompatible types: CommunityFragment cannot be converted to Fragment" ваш CommunityFragment расширяет AppCompatActivity и это не фрагмент.

...