无法动态添加视图

时间:2018-12-01 11:45:12

标签: android xml

我的问题是关于.addView()。我无法在LinearLayout中添加视图。我在两个不同的片段中有相同的代码。首先正确运行它。可能是因为有恒定的视图数。其次根本不添加视图。我猜.xml文件中存在一些问题。你能帮我吗?

package com.andrey.proparser;

import android.graphics.drawable.TransitionDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

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

import static android.support.constraint.Constraints.TAG;

public class SearchFragment extends Fragment {

    public static final String URL = "http://knijky.ru/search?search_api_views_fulltext=";

    int counter = 0;

    Animation anim1 = null;
    Animation anim2 = null;
    EditText editText;
    Button button;
    ImageView image;

    List<View> all_Boocks = new ArrayList<>();
    List<String> books_link = new ArrayList<>();
    List<String> book_names = new ArrayList<>();
    List<String> book_desription = new ArrayList<>();




    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, null);
        editText = view.findViewById(R.id.search_text);
        image = view.findViewById(R.id.search_image);
        anim1 = AnimationUtils.loadAnimation(getActivity(), R.anim.animation);
        anim2 = AnimationUtils.loadAnimation(getActivity(), R.anim.rising);
        final TransitionDrawable transition = (TransitionDrawable) image.getBackground();
        editText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN)
                {
                    switch (i)
                    {
                        case KeyEvent.KEYCODE_ENTER:
                            image.startAnimation(anim1);
                            editText.startAnimation(anim2);
                            transition.startTransition(650);
                            DoStaff();
                            return true;
                        default:
                            break;
                    }
                }
                return false;
            }
        });



        return view;
    }

    private class MyTask extends AsyncTask<Void,Void,Void> {
        String url = URL + editText.getText();
        @Override
        protected Void doInBackground(Void... voids) {

            try {
                Document doc = Jsoup.connect(url).get();
                Elements results = doc.select("span.field-content > a");
                Elements description = doc.select("div.views-field views-field-title > span.field-content" );
                for (Element link : results)
                    if(link.attr("href").contains("/books/")){
                        books_link.add("http://knijky.ru/" + link.attr("href"));
                        book_names.add(link.text());
                    }
                counter = books_link.size()/2;
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
           final LinearLayout linear = getLayoutInflater().inflate(R.layout.fragment_search, null).findViewById(R.id.linear_search);
            for (int i = 0; i < counter; i++) {
                final View view_read = getLayoutInflater().inflate(R.layout.read_book_button, null);
                TextView name = view_read.findViewById(R.id.bookName);
                name.setText(book_names.get(i));
                all_Boocks.add(view_read);
                linear.addView(view_read);
                Log.d(TAG, String.valueOf(i));
            }
            Log.d(TAG, String.valueOf(all_Boocks.size()));
        }


    }

    public void DoStaff(){
        new MyTask().execute();
    }



}

这是fragment_search.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <EditText
        android:id="@+id/search_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:layout_marginStart="30dp"
        android:hint="Для поиска нажмите Enter"/>


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/transition"
        android:id="@+id/search_image"
        android:layout_marginStart="30dp"
        android:layout_centerInParent="true"/>



    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/search_image">

        <LinearLayout
            android:id="@+id/linear_search"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">
        </LinearLayout>

    </ScrollView>

</RelativeLayout>

这是read_book_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="26dp"
        android:text="Some text"
        android:id="@+id/bookName"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/read_btn"
        android:layout_toLeftOf="@+id/read_btn"
        android:layout_marginLeft="20dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bookName"
        android:id="@+id/bookDescription"
        android:text="Description"
        android:layout_marginLeft="20dp"
        android:layout_toStartOf="@+id/read_btn"
        android:layout_toLeftOf="@+id/read_btn"
        android:layout_alignParentLeft="true"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@drawable/read_btn"
        android:id="@+id/read_btn"
        android:layout_marginTop="10dp"
        />
</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

尝试通过以下方式实施:

AFRAME.registerComponent('move', {
  schema: {
   speed: { type: 'number', default: 2 }
  },

  tick: function(t, dt) {
     var target = this.el.sceneEl.querySelector('#boats');
     var vec3 = new THREE.Vector3();
     var currentPosition = this.el.object3D.position;
     target.object3D.getWorldPosition(vec3);
     var  distance = dt*this.data.speed / 100;      
     this.el.object3D.translateOnAxis(vec3, distance);

     if (currentPosition.z > 30) {
        this.el.setAttribute('position', {
          z: -60
        });
      }

    }  
});

在这里我删除了您的逻辑,您可以像以前一样使用充气机来获得线性布局