SwipeStackAdapter适配器的日期选择器

时间:2017-02-09 10:42:10

标签: java android xml android-fragments android-datepicker

我用Google搜索了很多。但我没有得到解决方案。

我的问题是我有一个片段。在片段中,我正在给一些卡片充气,即刷卡。使用SwipeStackAdapter来扩展视图xml。

在我的一个xml视图中尝试实现datepicker。

TaskCardListShow.java片段代码

Insad.saveAs({
    url: url,
    method: 'GET',  // or use 'POST' if you really must ;)
    defaultFilename: defaultFilename
});

card_4.xml

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;

import com.jmedeisis.draglinearlayout.DragLinearLayout; 

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

import link.fls.swipestack.SwipeStack;

public class TaskCardListShow extends Fragment implements SwipeStack.SwipeStackListener, View.OnClickListener {
    private ArrayList<String> mData;
    private ArrayList<String> LayOutData;
    private SwipeStack mSwipeStack;
    private SwipeStackAdapter mAdapter;
    public int cardCounter;
    Context context;
    AutoCompleteTextView autoTextView;

    public TaskCardListShow() {
    }
    public static TaskCardListShow newInstance() {

        TaskCardListShow _TaskCardListShow = new TaskCardListShow();

        return _TaskCardListShow;
    }

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

        mSwipeStack = (SwipeStack) view.findViewById(R.id.swipeStack);
        mData = new ArrayList<>();
        LayOutData = new ArrayList<>();
        mAdapter = new SwipeStackAdapter(mData,LayOutData);
        mSwipeStack.setAdapter(mAdapter);
        mSwipeStack.setListener(this);
        context=getActivity().getApplicationContext();

        cardCounter=7;// write function for getting card count.

        fillStackCard(); 
        return view;
    }

    private void fillStackCard() {
        for (int x = 0; x <cardCounter; x++) {
            mData.add(getString(R.string.dummy_text) + " " + (x + 1));
            LayOutData.add(getString(R.string.str_card)+ (x + 1));
        }
    }


    @Override
    public void onClick(View v) {

    }
    @Override
    public void onViewSwipedToRight(int position) {
        String swipedElement = mAdapter.getItem(position);
    }

    @Override
    public void onViewSwipedToLeft(int position) {
        String swipedElement = mAdapter.getItem(position);
    }

    @Override
    public void onStackEmpty() {

        removeTaskCardFragment();
    }
    public void removeTaskCardFragment(){
        FragmentManager manager = getActivity().getSupportFragmentManager();
        FragmentTransaction mFragmentTransaction = manager.beginTransaction();
        TaskCardListShow _TaskCardListShowFragment = new TaskCardListShow();
        mFragmentTransaction.remove(_TaskCardListShowFragment);
        mFragmentTransaction.commit();
        manager.popBackStack();
    }

    public class SwipeStackAdapter extends BaseAdapter {

        private List<String> mData;
        private List<String> LayOutData;
        DragLinearLayout dragDropAndroidLinearLayout;

        public SwipeStackAdapter(List<String> data,List<String> Ldata) {
            this.mData = data;
            this.LayOutData = Ldata;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public String getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            String LName=LayOutData.get(position);

            try {
                int id = getActivity().getApplicationContext().getResources().
                        getIdentifier(LName, "layout", getActivity().getPackageName());
                convertView = getActivity().getLayoutInflater().inflate(id, parent, false);


                if(position==4){ // my date picker layout will come this postion.

                }
            } catch (Exception e) {
                e.printStackTrace();
            } 
            return convertView;
        }


}
你能指导我这样做吗?我已经尝试了很多方法,但它不适用于我的情况

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题,

我的适配器

final TextView _dpResult=(TextView)convertView.findViewById(R.id.dpResult);
_dpResult.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View arg0) {
                                DialogFragment newFragment = new SelectDateFragment( );
                                newFragment.show(getFragmentManager(), "DatePicker");
                            }
                        });

<强> SelectDateFragment

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm+1, dd);
    }
    public void populateSetDate(int year, int month, int day) {
        TextView dpResult= (TextView)getActivity().findViewById(R.id.dpResult);
        dpResult.setText(year+"-"+month+"-"+day);
    }
}

日期选择器的xml编码

        <TextView
        android:id="@+id/dpResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:hint="yyyy-mm-dd" />

现在我可以在卡片上看到日期选择器了。谢谢